(转)LinearLayout中实现水平方向上的两个Textview一个居左,一个居右的效果

原地址:http://blog.csdn.net/lb1207087645/article/details/49819453


我们知道,在LineraLayout中,当设置android:orientation="horizental" ,子控件的android:layout_gravity="left",android:layout_gravity="right"是无效的,所以我们可以改用比重的方法来达到两个textview一个居左,一个居右的效果,如下这种效果


<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView

        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="张三"/>

    <TextView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="李四"/>

</LinearLayout>

设置 android:layout_width="0dp",android:layout_height="wrap_content",android:layout_weight="1"这三个属性后,就会把match_parent剩下的布局给撑满,所以会把右边那个textView挤到最右端。
这样就能达到在LinLinearLayout中水平方向上的有两个textview一个居左,一个居右的效果。

Android开发,要实现LinearLayout一个ImageView而另一个居右的布局,可以通过设置布局参数来完成。LinearLayout支持权重(weight)分配,通过合理地设置权重和gravity属性,可以控制ImageView的位置。以下是实现该布局的基本步骤: 1. 确保你的LinearLayout的属性设置`android:orientation="horizontal"`,这样子视图将水平排列。 2. 为每个ImageView设置一个`layout_width`属性为`0dp`,并使用`layout_weight="1"`属性。这样,每个ImageView会尝试占据相同的空间,但实际大小取决于它们的内容。 3. 设置两个ImageView的`layout_gravity`属性为`left`和`right`。第一个ImageView的`layout_gravity`属性设置为`left`,而第二个ImageView的`layout_gravity`属性设置为`right`。这样可以确保它们分别靠和靠右对齐。 4. 根据需要设置`layout_height`属性,通常为了保持视图的一致性,会设置为`wrap_content`或者`match_parent`。 示例代码如下: ```xml <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="left" android:src="@drawable/image1" /> <ImageView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="right" android:src="@drawable/image2" /> </LinearLayout> ``` 在这个例子,`@drawable/image1`和`@drawable/image2`是你想要显示的图片资源的引用。确保你的ImageView引用了正确的图片资源。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值