(二)Android布局之相对布局(RelativeLayout)

蔚落 2022-08-06 14:17 361阅读 0赞

相对布局,直接粘代码main.xml示例代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <TextView
  6. android:layout_width="fill_parent"
  7. android:layout_height="wrap_content"
  8. android:id="@+id/text"
  9. android:text="@string/name_text"/>
  10. <EditText
  11. android:id="@+id/edit"
  12. android:layout_width="fill_parent"
  13. android:layout_height="wrap_content"
  14. android:layout_below="@id/text"/>
  15. <Button
  16. android:id="@+id/button1"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:layout_below="@id/edit"
  20. android:layout_alignParentRight="true"
  21. android:text="@string/cancle_button"/>
  22. <Button
  23. android:id="@+id/button2"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:text="@string/ok_button"
  27. android:layout_below="@id/edit"
  28. android:layout_toLeftOf="@id/button1"/>
  29. </RelativeLayout>

代码解析:

(1)android:layout_below=”@id/text”——该元素放到id为text的元素的下面;

(2)android:layout_toLeftOf=”@id/ok”——该元素放到id位ok的元素的左边;

(3)android:layout_alignTop=”@id/ok”——对齐id为ok的元素的顶部;

界面效果如下:

Center

线性布局与相对布局嵌套使用:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <TextView
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:text="@string/name_text"/>
  10. <EditText
  11. android:id="@+id/edit"
  12. android:layout_width="fill_parent"
  13. android:layout_height="wrap_content"
  14. android:hint="#####"/>
  15. <RelativeLayout
  16. android:layout_width="fill_parent"
  17. android:layout_height="wrap_content">
  18. <Button
  19. android:id="@+id/button1"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:text="@string/cancle_button"/>
  23. <Button
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:layout_toRightOf="@id/button1"
  27. android:text="@string/ok_button"/>
  28. </RelativeLayout>
  29. </LinearLayout>

效果图如下:

Center 1

发表评论

表情:
评论列表 (有 0 条评论,361人围观)

还没有评论,来说两句吧...

相关阅读