Android四種布局

LinearLayout

<LinearLayout xmlns ="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
/LinearLayout>

android:orientation="vertical" #horizontal
android:layout_gravity   #指定控件在布局中的對稱方式
#top、bottom、center_vertical、
android:gravity              #指定文字在控件中的對齊方式

android:layout_weight = 1
#設(shè)置layout_weight的時(shí)候可以設(shè)置layout_width=0,系統(tǒng)會自己計(jì)算

RelativeLayout

<RelativeLayout>
</relativeLayout>
#相對于父控件
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"

android:layout_centerInParent="true"

#相對于兄弟控件,上、下、左、右
android:layout_above="@id/..."
android:layout_toRightOf = "@id/..."
android:layout_toLeftOf = "@id/..."
android:layout_below = "@id/..."

#對齊屬性
android:layout_alignLeft="@id/.." #左對齊
android:layout_alignRight="@id/..."#右對齊
android:layout_alignTop
android:layout_alignBottom

FrameLayout

<FrameLayout>
</FrameLayout>
都是位于控件的左上角

TableLayout

<TableLayout>
</TableLayout>

#一個(gè)TableRow內(nèi)的內(nèi)容在一行內(nèi)
<TableRow>
</TableRow>
#TableRow內(nèi)的控件是不能指定寬度的

android:layout_spam="2" #指定占用兩列

#設(shè)置TableLayout的android:stretchColumns="1",表示如果表格不能完全占滿屏幕寬度,就將第二列拉伸
#如果="0",代表拉伸第一列

android:inputType="textPassword"
#引入布局
#有一個(gè)布局文件title.xml
<include layout="@layout/title" />

創(chuàng)建自定義控件

public class TitleLayout extends LinearLayout {
  public TitleLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    layoutInflater.from(context).inflate(R.layout.title, this);
    Button titleBack = (Button) findViewById(R.id.title_back);
     title_back.setOnclickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        ((Activity) getContext()).finish();
       }
    }) 
  }
}
修改
<include layout="@layout/title" />
改為
<com.example.xujiguang.TitleLayout
  android:layout_weight="match_parent"
  android:layout_height="wrap_content"
 ></com.example.xujiguang.TitleLayout>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容