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

在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中实现水平方向控件居右

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容