Android技术集Android开发Android开发经验谈

Android LinearLayout中实现水平方向控件居右

2017-09-12  本文已影响111人  Swift社区

在LineraLayout中,当设置

android:orientation="horizental" 

子控件的

android:layout_gravity="left"
android:layout_gravity="right"

是无效的,所以我们可以改用比重的方法来达到两个控件一个居左,一个居右的效果,如下这种效果:

控件效果

代码如下:

<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="请输入短信验证码"
                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"

这三个属性后,就会把match_parent剩下的布局给撑满,所以会把右边那个Button挤到最右端。
这样就可以解决LinearLayout中实现水平方向控件居右

希望可以帮助大家
如果哪里有什么不对或者不足的地方,还望读者多多提意见或建议
Android技术交流群:591625129

上一篇 下一篇

猜你喜欢

热点阅读