左边textview过长时显示...

方案一:重写RelativeLayout.onMeasure方法

public class AddressLayout extends RelativeLayout
{

	public AddressLayout(Context context, AttributeSet attrs)
	{
		super(context, attrs);
	}

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();

        View child1 = findViewById(R.id.item_address_name);
        View child2 = findViewById(R.id.layout_right);
        if(child1.getMeasuredWidth() + child2.getMeasuredWidth() < width)
        {
            setMeasuredDimension(child1.getMeasuredWidth() + child2.getMeasuredWidth(), height);
        }
    }
}
 <com.qianwang.qianbao.im.ui.set.AddressLayout
        android:id="@+id/layout_top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="left">

        <LinearLayout
            android:id="@+id/layout_right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:gravity="left"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/item_address_mobile"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:textColor="@color/common_gray_black_text_color"
                android:textSize="@dimen/common_font_size_16" />

            <TextView
                android:id="@+id/tv_default"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/btn_shop_green"
                android:paddingLeft="2dp"
                android:paddingRight="2dp"
                android:text="@string/delivery_address_default"
                android:textColor="@color/white" />
        </LinearLayout>

        <TextView
            android:id="@+id/item_address_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/layout_right"
            android:ellipsize="end"
            android:gravity="left"
            android:singleLine="true"
            android:textColor="@color/common_gray_black_text_color"
            android:textSize="@dimen/common_font_size_16" />
    </com.qianwang.qianbao.im.ui.set.AddressLayout>

方案二:用LinearLayout

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center_vertical">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="1.0"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:singleLine="true"
            android:textSize="13sp"
            android:textColor="@color/text_color_3"
            android:text="位置位置位置位位置位位置位置位置"/>
    </LinearLayout>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="4dp"
        android:paddingRight="10dp"
        android:src="@drawable/bbs_topic_del2" />
</LinearLayout>

 

另外,一行居中,多行居左的TextView

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- 套一层wrap_content的ViewGroup -->
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true">
 
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />
    </LinearLayout>
</RelativeLayout>

 

转载于:https://my.oschina.net/bruces/blog/535679

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值