Android入門(一)

來源:互聯網
上載者:User

標籤:nbsp   color   main   put   水平   contex   空間   family   size   

線性布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="44dp"    android:orientation="horizontal">    <TextView        android:layout_width="match_parent"        android:layout_height="44dp"        android:layout_gravity="center"        android:background="#ff0000"        android:ellipsize="end"        android:layout_weight="1"        android:gravity="center"        android:singleLine="true"        android:text="111111111111111111111111" />    <TextView        android:layout_width="match_parent"        android:layout_gravity="center"        android:layout_weight="1"        android:background="#000000"        android:text="2222222222222" />    <TextView        android:layout_width="match_parent"        android:layout_height="44dp"        android:layout_gravity="center"        android:background="#ff0000"        android:layout_weight="1"        android:ellipsize="end"        android:gravity="center"        android:singleLine="true"        android:text="111111111111" /></LinearLayout>

給每個TextView設定layout_weight=1屬性平分父級空間。可以做到文字超出隱藏。

或是

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="44dp"    android:orientation="horizontal">    <TextView        android:layout_width="wrap_content"        android:layout_height="44dp"        android:layout_gravity="center"        android:background="#ff0000"        android:gravity="center"        android:text="111111111111111111111111" />    <TextView        android:layout_width="match_parent"        android:layout_gravity="center"        android:layout_weight="1"        android:background="#000000"        android:text="2222222222222" />    <TextView        android:layout_width="wrap_content"        android:layout_height="44dp"        android:layout_gravity="center"        android:background="#ff0000"        android:gravity="center"        android:text="111111111111" /></LinearLayout>

只給中間佔位的TextView設定android:layout_weight=1,撐開空間做到分離左右TextView。此時左右的TextView的寬度為wrap_content,所以如果哪個TrxtView內容過多的話,會佔滿整個父元素。

或是

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/mRelativeLayout"    android:layout_width="match_parent"    android:layout_height="match_parent">    <EditText        android:id="@+id/input_message"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_weight="1"        android:hint="Type something" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="button" /></LinearLayout>

 

除去一邊元素寬度,左邊自適應寬度。

layout_weight的計算方式同flexbox一致。

 相對布局
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true"        android:text="button 1" />    <Button        android:id="@+id/button2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:layout_alignParentTop="true"        android:text="button 2" />    <Button        android:id="@+id/button3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:text="button 3" />    <Button        android:id="@+id/button4"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_alignParentLeft="true"        android:text="button 4" />    <Button        android:id="@+id/button5"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_alignParentRight="true"        android:text="button 5"        android:textAllCaps="false" /></RelativeLayout>

子控制項相對於父容器

android:layout_alignParentBottom 如果該值為true,則將該控制項的底部和父控制項的底部對齊
android:layout_alignParentLeft 如果該值為true,則將該控制項的左邊與父控制項的左邊對齊
android:layout_alignParentRight 如果該值為true,則將該控制項的右邊與父控制項的右邊對齊
android:layout_alignParentTop 如果該值為true,則將空間的頂部與父控制項的頂部對齊
android:layout_centerHorizontal 如果值為true,該控制項將被至於水平方向的中央
android:layout_centerInParent 如果值為true,該控制項將被至於父控制項水平方向和垂直方向的中央
android:layout_centerVertical 如果值為true,該控制項將被至於垂直方向的中央

子控制項相對於子控制項

android:layout_above 在給定ID控制項的上面
android:ayout_below在給定ID控制項的下面
android:layout_toLeftOf 在給定ID控制項的左邊
android:layout_toRightOf 在給定ID控制項的右邊
android:layout_alignBottom 與給定ID控制項的底部邊緣對齊
android:layout_alignLeft 與給定ID控制項的左邊緣對齊
android:layout_alignRight 與給定ID控制項的右邊緣對齊
android:layout_alignTop 與給定ID控制項的上邊緣對齊

約束布局
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.thinkpad.testandroid.Main4Activity">    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Demo"/>    <Button        android:id="@+id/button2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="button2"        app:layout_constraintLeft_toRightOf="@id/button1"/>    <Button        android:id="@+id/button3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginBottom="20dp"        android:text="button3"        app:layout_constraintBottom_toBottomOf="parent"/></android.support.constraint.ConstraintLayout>

屬性形如layout_constraintXXX_toYYYOf,constraintXXX裡的XXX代表是這個子控制項自身的哪條邊(Left、Right、Top、Bottom、Baseline,而toYYYOf裡的YYY代表的是和約束控制項的哪條邊發生約束(取值同樣是Left、Right、Top、Bottom、Baseline)。

例如:app:layout_constraintLeft_toRightOf="@id/button1",表示的是控制項自身的左側在button1的右側;                app:layout_constraintLeft_toRightOf="parent",表示的是控制項自身的底部和父控制項的底部對齊。

Android入門(一)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.