在LineraLayout中,當(dāng)設(shè)置
android:orientation="horizental"
子控件的
android:layout_gravity="left"
android:layout_gravity="right"
是無效的,所以我們可以改用比重的方法來達(dá)到兩個(gè)控件一個(gè)居左,一個(gè)居右的效果,如下這種效果:

控件效果
代碼如下:
<LinearLayout android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/white"
android:orientation="horizontal" >
<EditText
android:id="@+id/phonetext"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginLeft="15dp"
android:layout_gravity="center_vertical"
android:inputType="number"
android:hint="請(qǐng)輸入短信驗(yàn)證碼"
android:background="@null"/>
<Button
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:textSize="16dp"
android:background="@drawable/tv_timemessage_bg"
android:text="57秒"
/>
</LinearLayout>
代碼分析:
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
這三個(gè)屬性后,就會(huì)把match_parent剩下的布局給撐滿,所以會(huì)把右邊那個(gè)Button擠到最右端。
這樣就可以解決LinearLayout中實(shí)現(xiàn)水平方向控件居右
希望可以幫助大家
如果哪里有什么不對(duì)或者不足的地方,還望讀者多多提意見或建議