Android LinearLayout中實(shí)現(xiàn)水平方向控件居右

在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ì)或者不足的地方,還望讀者多多提意見或建議

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容