首页 > 代码库 > Android 开发笔记___RelativeLayout
Android 开发笔记___RelativeLayout
xml文件实现
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="500dp" > 4 5 <Button 6 android:id="@+id/btn_center" 7 style="@style/btn_relative" 8 android:layout_centerInParent="true" 9 android:text="我在中间" /> 10 11 <Button 12 android:id="@+id/btn_center_horizontal" 13 style="@style/btn_relative" 14 android:layout_centerHorizontal="true" 15 android:text="我在水平中间" /> 16 17 <Button 18 android:id="@+id/btn_center_vertical" 19 style="@style/btn_relative" 20 android:layout_centerVertical="true" 21 android:text="我在垂直中间" /> 22 23 <Button 24 android:id="@+id/btn_parent_left" 25 style="@style/btn_relative" 26 android:layout_marginTop="100dp" 27 android:layout_alignParentLeft="true" 28 android:text="我跟上级左边对齐" /> 29 30 <Button 31 android:id="@+id/btn_parent_top" 32 style="@style/btn_relative" 33 android:layout_width="120dp" 34 android:layout_alignParentTop="true" 35 android:text="我跟上级顶部对齐" /> 36 37 <Button 38 android:id="@+id/btn_parent_right" 39 style="@style/btn_relative" 40 android:layout_marginTop="100dp" 41 android:layout_alignParentRight="true" 42 android:text="我跟上级右边对齐" /> 43 44 <Button 45 android:id="@+id/btn_parent_bottom" 46 style="@style/btn_relative" 47 android:layout_width="120dp" 48 android:layout_alignParentBottom="true" 49 android:layout_centerHorizontal="true" 50 android:text="我跟上级底部对齐" /> 51 52 <Button 53 android:id="@+id/btn_left_bottom" 54 style="@style/btn_relative" 55 android:layout_toLeftOf="@+id/btn_parent_bottom" 56 android:layout_alignTop="@+id/btn_parent_bottom" 57 android:text="我在底部左边" /> 58 59 <Button 60 android:id="@+id/btn_right_bottom" 61 style="@style/btn_relative" 62 android:layout_toRightOf="@+id/btn_parent_bottom" 63 android:layout_alignBottom="@+id/btn_parent_bottom" 64 android:text="我在底部右边" /> 65 66 <Button 67 android:id="@+id/btn_above_center" 68 style="@style/btn_relative" 69 android:layout_above="@+id/btn_center" 70 android:layout_alignLeft="@+id/btn_center" 71 android:text="我在中间上面" /> 72 73 <Button 74 android:id="@+id/btn_below_center" 75 style="@style/btn_relative" 76 android:layout_below="@+id/btn_center" 77 android:layout_alignRight="@+id/btn_center" 78 android: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 17 protected void onCreate(@Nullable Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.code_3_1_1); 20 } 21 22 public static void startHome(Context mContext) { 23 Intent intent = new Intent(mContext, class_3_1_1.class); 24 mContext.startActivity(intent); 25 } 26 }
代码实现
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:id="@+id/rl_content" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" > 5 6 <TextView 7 style="@style/btn_relative" 8 android:layout_alignParentTop="true" 9 android:layout_centerHorizontal="true" 10 android:text="点击按钮添加视图,长按视图删除自身" /> 11 12 <Button 13 android:id="@+id/btn_add_left" 14 style="@style/btn_relative" 15 android:layout_marginTop="80dp" 16 android:layout_centerHorizontal="true" 17 android:text="添加左边视图" /> 18 19 <Button 20 android:id="@+id/btn_add_above" 21 style="@style/btn_relative" 22 android:layout_below="@+id/btn_add_left" 23 android:layout_alignLeft="@+id/btn_add_left" 24 android:text="添加上方视图" /> 25 26 <Button 27 android:id="@+id/btn_add_right" 28 style="@style/btn_relative" 29 android:layout_below="@+id/btn_add_above" 30 android:layout_alignLeft="@+id/btn_add_left" 31 android:text="添加右边视图" /> 32 33 <Button 34 android:id="@+id/btn_add_below" 35 style="@style/btn_relative" 36 android:layout_below="@+id/btn_add_right" 37 android:layout_alignLeft="@+id/btn_add_left" 38 android:text="添加下方视图" /> 39 40 <Button 41 android:id="@+id/btn_add_center" 42 style="@style/btn_relative" 43 android:layout_below="@+id/btn_add_below" 44 android:layout_alignLeft="@+id/btn_add_left" 45 android:text="添加中间视图" /> 46 47 <Button 48 android:id="@+id/btn_add_parent_left" 49 style="@style/btn_relative" 50 android:layout_below="@+id/btn_add_center" 51 android:layout_alignLeft="@+id/btn_add_left" 52 android:text="添加上级左侧对齐视图" /> 53 54 <Button 55 android:id="@+id/btn_add_parent_top" 56 style="@style/btn_relative" 57 android:layout_below="@+id/btn_add_parent_left" 58 android:layout_alignLeft="@+id/btn_add_left" 59 android:text="添加上级顶部对齐视图" /> 60 61 <Button 62 android:id="@+id/btn_add_parent_right" 63 style="@style/btn_relative" 64 android:layout_below="@+id/btn_add_parent_top" 65 android:layout_alignLeft="@+id/btn_add_left" 66 android:text="添加上级右侧对齐视图" /> 67 68 <Button 69 android:id="@+id/btn_add_parent_bottom" 70 style="@style/btn_relative" 71 android:layout_below="@+id/btn_add_parent_right" 72 android:layout_alignLeft="@+id/btn_add_left" 73 android: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 17 private RelativeLayout rl_content; 18 19 @Override 20 protected void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.code_3_1_1_code); 23 24 rl_content = (RelativeLayout) findViewById(R.id.rl_content); 25 findViewById(R.id.btn_add_left).setOnClickListener(this); 26 findViewById(R.id.btn_add_above).setOnClickListener(this); 27 findViewById(R.id.btn_add_right).setOnClickListener(this); 28 findViewById(R.id.btn_add_below).setOnClickListener(this); 29 findViewById(R.id.btn_add_center).setOnClickListener(this); 30 findViewById(R.id.btn_add_parent_left).setOnClickListener(this); 31 findViewById(R.id.btn_add_parent_top).setOnClickListener(this); 32 findViewById(R.id.btn_add_parent_right).setOnClickListener(this); 33 findViewById(R.id.btn_add_parent_bottom).setOnClickListener(this); 34 } 35 36 @Override 37 public void onClick(View v) { 38 if (v.getId() == R.id.btn_add_left) { 39 addNewView(RelativeLayout.LEFT_OF, RelativeLayout.ALIGN_TOP, v.getId()); 40 } else if (v.getId() == R.id.btn_add_above) { 41 addNewView(RelativeLayout.ABOVE, RelativeLayout.ALIGN_LEFT, v.getId()); 42 } else if (v.getId() == R.id.btn_add_right) { 43 addNewView(RelativeLayout.RIGHT_OF, RelativeLayout.ALIGN_BOTTOM, v.getId()); 44 } else if (v.getId() == R.id.btn_add_below) { 45 addNewView(RelativeLayout.BELOW, RelativeLayout.ALIGN_RIGHT, v.getId()); 46 } else if (v.getId() == R.id.btn_add_center) { 47 addNewView(RelativeLayout.CENTER_IN_PARENT, -1, rl_content.getId()); 48 } else if (v.getId() == R.id.btn_add_parent_left) { 49 addNewView(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.CENTER_VERTICAL, rl_content.getId()); 50 } else if (v.getId() == R.id.btn_add_parent_top) { 51 addNewView(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.CENTER_HORIZONTAL, rl_content.getId()); 52 } else if (v.getId() == R.id.btn_add_parent_right) { 53 addNewView(RelativeLayout.ALIGN_PARENT_RIGHT, -1, rl_content.getId()); 54 } else if (v.getId() == R.id.btn_add_parent_bottom) { 55 addNewView(RelativeLayout.ALIGN_PARENT_BOTTOM, -1, rl_content.getId()); 56 } 57 } 58 59 private void addNewView(int firstAlign, int secondAlign, int referId) { 60 View v = new View(this); 61 v.setBackgroundColor(0xaa66ff66); 62 RelativeLayout.LayoutParams rl_params = new RelativeLayout.LayoutParams(100, 100); 63 rl_params.addRule(firstAlign, referId); 64 if (secondAlign >= 0) { 65 rl_params.addRule(secondAlign, referId); 66 } 67 v.setLayoutParams(rl_params); 68 v.setOnLongClickListener(new View.OnLongClickListener() { 69 @Override 70 public boolean onLongClick(View vv) { 71 rl_content.removeView(vv); 72 return true; 73 } 74 }); 75 rl_content.addView(v); 76 } 77 78 public static void startHome(Context mContext) { 79 Intent intent = new Intent(mContext, class_3_1_1_code.class); 80 mContext.startActivity(intent); 81 } 82 83 }
Android 开发笔记___RelativeLayout
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。