布局中的一些小技巧

1:LinearLayput  线性布局

线性布局中设置控件一个居左一个居右,设置一个控件用于占位,设置这个控件 的权重为1,。具体你代码如下

<LinearLayout
    android:background="@color/color_ffe89a"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:layout_gravity="center"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:text="登录"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:gravity="center_vertical"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:text="注册"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

  2:RelativeLayout   相对布局

相对布局设置一个控件为自包裹,另一个控件占据剩下的所有位置,为另一个控件设置他的宽度的math_parent,代码如下

 <RelativeLayout
        android:background="@color/orange"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/left"
            android:text="左边"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:background="@color/colorf5"
            android:gravity="center"
            android:layout_toRightOf="@+id/left"
            android:text="右边"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </RelativeLayout>

  3.设置布局中的顶部导航,不会随着页面的滑动而改变位置

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorGrayF3"
    android:orientation="vertical">
//顶部导航 <com.egongchang.egc.view.EgcTitleView android:id="@+id/egctitle_myadvice_or_promotion" android:layout_width="match_parent" android:layout_height="wrap_content" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent">
//带有刷新和加载的listview <com.handmark.pulltorefresh.library.PullToRefreshListView
xmlns:ptr="http://schemas.android.com/apk/res-auto" android:id="@+id/mhyadvice_or_promotion_listview" android:layout_width="match_parent"
//布局中的高度是整个父装体 android:layout_height="match_parent" android:layout_above="@+id/has_choose" android:cacheColorHint="@color/white" android:divider="@color/colorGrayF3" android:dividerHeight="1dip" android:fadingEdge="none" android:fastScrollEnabled="false" android:headerDividersEnabled="false" android:smoothScrollbar="true" android:visibility="visible" ptr:ptrMode="both" /> </RelativeLayout> </LinearLayout>

  

posted @ 2017-07-29 10:45  让学习如呼吸一般自然  阅读(176)  评论(0编辑  收藏  举报