android-凯发k8官方网
凯发k8官方网
收集整理的这篇文章主要介绍了
android-relativelayout布局技巧(一)
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
如果有一个需求是这样的,在标题中的右上角有一个button
1 xml version="1.0" encoding="utf-8"?> 2 <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 <relativelayout 7 android:layout_width="match_parent" 8 android:layout_height="45dp" 9 android:background="@color/pink_light"> 10 <textview 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:text="标题" 14 android:layout_centerinparent="true" 15 /> 16 17 <button 18 android:layout_width="45dp" 19 android:layout_height="26dp" 20 android:layout_alignparentright="true" 21 android:layout_marginright="10dp" 22 android:layout_centervertical="true" 23 android:background="@color/green_light" 24 android:text="更多" 25 android:textsize="12sp" 26 /> 27 relativelayout> 28 linearlayout>其实上面的例子用到了相对布局的相对父控件,居中,靠右,距离右边多少db。当然相对父控件比较多,我们也给你自己定义一个参照物,比如,我们可以相对标题中的textview作为参照物
1 xml version="1.0" encoding="utf-8"?> 2 <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 <relativelayout 7 android:layout_width="match_parent" 8 android:layout_height="45dp" 9 android:background="@color/pink_light"> 10 <textview 11 android:id="@ id/title" 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:text="标题" 15 android:layout_centerinparent="true" 16 /> 17 <textview 18 android:layout_width="wrap_content" 19 android:layout_height="wrap_content" 20 android:text="标题2" 21 android:layout_torightof="@ id/title" 22 android:layout_centerinparent="true" 23 android:layout_marginleft="5dp" 24 /> 25 relativelayout> 26 linearlayout>居中参照物
1 <relativelayout 2 android:layout_width="match_parent" 3 android:background="@color/white" 4 android:layout_gravity="center_horizontal" 5 android:layout_height="wrap_content" 6 android:layout_margintop="10dp" 7 > 8 <button 9 android:id="@ id/common_dialog_surebtn" 10 android:layout_width="@dimen/common_button_width" 11 android:layout_height="@dimen/common_button_height" 12 android:background="@drawable/common_click_bule_btn_selector" 13 android:text="@string/sure" 14 android:gravity="center" 15 android:textcolor="@color/white" 16 android:textsize="@dimen/common_words_size" 17 android:layout_marginright="30dp" 18 android:layout_toleftof="@ id/target_mid"/> 19 <view 20 android:id="@ id/target_mid" 21 android:layout_width="0dp" 22 android:layout_height="0dp" 23 android:layout_centerhorizontal="true" 24 /> 25 <button 26 android:id="@ id/common_dialog_canclebtn" 27 android:layout_width="@dimen/common_button_width" 28 android:layout_height="@dimen/common_button_height" 29 android:background="@drawable/common_click_gray_btn_selector" 30 android:text="@string/cencle" 31 android:gravity="center" 32 android:textcolor="@color/white" 33 android:layout_torightof="@ id/target_mid" 34 android:layout_marginleft="30dp" 35 android:textsize="@dimen/common_words_size"/> 36 relativelayout>此篇主要是 相对参照物
转载于:https://www.cnblogs.com/royi123/p/4265412.html
总结
以上是凯发k8官方网为你收集整理的android-relativelayout布局技巧(一)的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: linux学习笔记6
- 下一篇: ios设计模式(03):工厂方法