方案一:重写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>