大鹏一日同风起,扶摇直上九万里。这篇文章主要讲述Android 开发笔记___RelativeLayout相关的知识,希望能为你提供帮助。
xml文件实现
1 < RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2android:layout_width="match_parent" 3android:layout_height="500dp" > 4 5< Button 6android:id="@+id/btn_center" 7style="@style/btn_relative" 8android:layout_centerInParent="true" 9android:text="我在中间" /> 10 11< Button 12android:id="@+id/btn_center_horizontal" 13style="@style/btn_relative" 14android:layout_centerHorizontal="true" 15android:text="我在水平中间" /> 16 17< Button 18android:id="@+id/btn_center_vertical" 19style="@style/btn_relative" 20android:layout_centerVertical="true" 21android:text="我在垂直中间" /> 22 23< Button 24android:id="@+id/btn_parent_left" 25style="@style/btn_relative" 26android:layout_marginTop="100dp" 27android:layout_alignParentLeft="true" 28android:text="我跟上级左边对齐" /> 29 30< Button 31android:id="@+id/btn_parent_top" 32style="@style/btn_relative" 33android:layout_width="120dp" 34android:layout_alignParentTop="true" 35android:text="我跟上级顶部对齐" /> 36 37< Button 38android:id="@+id/btn_parent_right" 39style="@style/btn_relative" 40android:layout_marginTop="100dp" 41android:layout_alignParentRight="true" 42android:text="我跟上级右边对齐" /> 43 44< Button 45android:id="@+id/btn_parent_bottom" 46style="@style/btn_relative" 47android:layout_width="120dp" 48android:layout_alignParentBottom="true" 49android:layout_centerHorizontal="true" 50android:text="我跟上级底部对齐" /> 51 52< Button 53android:id="@+id/btn_left_bottom" 54style="@style/btn_relative" 55android:layout_toLeftOf="@+id/btn_parent_bottom" 56android:layout_alignTop="@+id/btn_parent_bottom" 57android:text="我在底部左边" /> 58 59< Button 60android:id="@+id/btn_right_bottom" 61style="@style/btn_relative" 62android:layout_toRightOf="@+id/btn_parent_bottom" 63android:layout_alignBottom="@+id/btn_parent_bottom" 64android:text="我在底部右边" /> 65 66< Button 67android:id="@+id/btn_above_center" 68style="@style/btn_relative" 69android:layout_above="@+id/btn_center" 70android:layout_alignLeft="@+id/btn_center" 71android:text="我在中间上面" /> 72 73< Button 74android:id="@+id/btn_below_center" 75style="@style/btn_relative" 76android:layout_below="@+id/btn_center" 77android:layout_alignRight="@+id/btn_center" 78android:text="我在中间下面" /> 79 80 < /RelativeLayout>
文章图片
java
1 package com.example.alimjan.hello_world; 2 3 import android.content.Context; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.support.annotation.Nullable; 7 import android.support.v7.app.AppCompatActivity; 8 9 import com.example.alimjan.hello_world.two.class__2_1; 10 11 /** 12* Created by alimjan on 7/2/2017. 13*/ 14 15 public class class_3_1_1 extends AppCompatActivity{ 16@Override 17protected void onCreate(@Nullable Bundle savedInstanceState) { 18super.onCreate(savedInstanceState); 19setContentView(R.layout.code_3_1_1); 20} 21 22public static void startHome(Context mContext) { 23Intent intent = new Intent(mContext, class_3_1_1.class); 24mContext.startActivity(intent); 25} 26 }
代码实现
1 < RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2android:id="@+id/rl_content" 3android:layout_width="match_parent" 4android:layout_height="match_parent" > 5 6< TextView 7style="@style/btn_relative" 8android:layout_alignParentTop="true" 9android:layout_centerHorizontal="true" 10android:text="点击按钮添加视图,长按视图删除自身" /> 11 12< Button 13android:id="@+id/btn_add_left" 14style="@style/btn_relative" 15android:layout_marginTop="80dp" 16android:layout_centerHorizontal="true" 17android:text="添加左边视图" /> 18 19< Button 20android:id="@+id/btn_add_above" 21style="@style/btn_relative" 22android:layout_below="@+id/btn_add_left" 23android:layout_alignLeft="@+id/btn_add_left" 24android:text="添加上方视图" /> 25 26< Button 27android:id="@+id/btn_add_right" 28style="@style/btn_relative" 29android:layout_below="@+id/btn_add_above" 30android:layout_alignLeft="@+id/btn_add_left" 31android:text="添加右边视图" /> 32 33< Button 34android:id="@+id/btn_add_below" 35style="@style/btn_relative" 36android:layout_below="@+id/btn_add_right" 37android:layout_alignLeft="@+id/btn_add_left" 38android:text="添加下方视图" /> 39 40< Button 41android:id="@+id/btn_add_center" 42style="@style/btn_relative" 43android:layout_below="@+id/btn_add_below" 44android:layout_alignLeft="@+id/btn_add_left" 45android:text="添加中间视图" /> 46 47< Button 48android:id="@+id/btn_add_parent_left" 49style="@style/btn_relative" 50android:layout_below="@+id/btn_add_center" 51android:layout_alignLeft="@+id/btn_add_left" 52android:text="添加上级左侧对齐视图" /> 53 54< Button 55android:id="@+id/btn_add_parent_top" 56style="@style/btn_relative" 57android:layout_below="@+id/btn_add_parent_left" 58android:layout_alignLeft="@+id/btn_add_left" 59android:text="添加上级顶部对齐视图" /> 60 61< Button 62android:id="@+id/btn_add_parent_right" 63style="@style/btn_relative" 64android:layout_below="@+id/btn_add_parent_top" 65android:layout_alignLeft="@+id/btn_add_left" 66android:text="添加上级右侧对齐视图" /> 67 68< Button 69android:id="@+id/btn_add_parent_bottom" 70style="@style/btn_relative" 71android:layout_below="@+id/btn_add_parent_right" 72android:layout_alignLeft="@+id/btn_add_left" 73android:text="添加上级底部对齐视图" /> 74 75 < /RelativeLayout>
文章图片
1 package com.example.alimjan.hello_world; 2 3 import android.content.Context; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.support.v7.app.AppCompatActivity; 7 import android.view.View; 8 import android.widget.RelativeLayout; 9 10 /** 11* Created by alimjan on 7/2/2017. 12*/ 13 14 15 public class class_3_1_1_code extends AppCompatActivity implements View.OnClickListener { 16 17private RelativeLayout rl_content; 18 19@Override 20protected void onCreate(Bundle savedInstanceState) { 21super.onCreate(savedInstanceState); 22setContentView(R.layout.code_3_1_1_code); 23 24rl_content = (RelativeLayout) findViewById(R.id.rl_content); 25findViewById(R.id.btn_add_left).setOnClickListener(this); 26findViewById(R.id.btn_add_above).setOnClickListener(this); 27findViewById(R.id.btn_add_right).setOnClickListener(this); 28findViewById(R.id.btn_add_below).setOnClickListener(this); 29findViewById(R.id.btn_add_center).setOnClickListener(this); 30findViewById(R.id.btn_add_parent_left).setOnClickListener(this); 31findViewById(R.id.btn_add_parent_top).setOnClickListener(this); 32findViewById(R.id.btn_add_parent_right).setOnClickListener(this); 33findViewById(R.id.btn_add_parent_bottom).setOnClickListener(this); 34} 35 36@Override 37public void onClick(View v) { 38if (v.getId() == R.id.btn_add_left) { 39addNewView(RelativeLayout.LEFT_OF, RelativeLayout.ALIGN_TOP, v.getId()); 40} else if (v.getId() == R.id.btn_add_above) { 41addNewView(RelativeLayout.ABOVE, RelativeLayout.ALIGN_LEFT, v.getId()); 42} else if (v.getId() == R.id.btn_add_right) { 43addNewView(RelativeLayout.RIGHT_OF, RelativeLayout.ALIGN_BOTTOM, v.getId()); 44} else if (v.getId() == R.id.btn_add_below) { 45addNewView(RelativeLayout.BELOW, RelativeLayout.ALIGN_RIGHT, v.getId()); 46} else if (v.getId() == R.id.btn_add_center) { 47addNewView(RelativeLayout.CENTER_IN_PARENT, -1, rl_content.getId()); 48} else if (v.getId() == R.id.btn_add_parent_left) { 49addNewView(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.CENTER_VERTICAL, rl_content.getId()); 50} else if (v.getId() == R.id.btn_add_parent_top) { 51addNewView(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.CENTER_HORIZONTAL, rl_content.getId()); 52} else if (v.getId() == R.id.btn_add_parent_right) { 53addNewView(RelativeLayout.ALIGN_PARENT_RIGHT, -1, rl_content.getId()); 54} else if (v.getId() == R.id.btn_add_parent_bottom) { 55addNewView(RelativeLayout.ALIGN_PARENT_BOTTOM, -1, rl_content.getId()); 56} 57} 58 59private void addNewView(int firstAlign, int secondAlign, int referId) { 60View v = new View(this); 61v.setBackgroundColor(0xaa66ff66); 62RelativeLayout.LayoutParams rl_params = new RelativeLayout.LayoutParams(100, 100); 63rl_params.addRule(firstAlign, referId); 64if (secondAlign > = 0) { 65rl_params.addRule(secondAlign, referId); 66} 67v.setLayoutParams(rl_params); 68v.setOnLongClickListener(new View.OnLongClickListener() { 69@Override 70public boolean onLongClick(View vv) { 71rl_content.removeView(vv); 72return true; 73} 74}); 75rl_content.addView(v); 76} 77 78public static void startHome(Context mContext) { 79Intent intent = new Intent(mContext, class_3_1_1_code.class); 80mContext.startActivity(intent); 81} 82 83 }
【Android 开发笔记___RelativeLayout】
推荐阅读
- Android Studio使用手记
- [android开发篇][android studio 和elipse都需要的流程 android sdk的安装
- Android开发之怎样监听让Service不被杀死
- [android开发篇]elicpse安装教程
- [android开发篇]java环境配置
- Android
- android 细节之An internal error occurred during: &quot;Launching New_configuration&quot;.
- idea和androidstudio的首次git配置一些问题
- Android Okhttp完美同步持久Cookie实现免登录