我该如何设置水平 LinearLayout
的最大宽度?如果内容很短(比如一些文本),布局会缩小,如果内容较长,则不会超过某个最大宽度值。
我更喜欢在 XML 层面上进行这个操作。
我该如何设置水平 LinearLayout
的最大宽度?如果内容很短(比如一些文本),布局会缩小,如果内容较长,则不会超过某个最大宽度值。
我更喜欢在 XML 层面上进行这个操作。
这就是我需要的——在之前的答案中所建议的基础上,进一步添加另一个布局(@+id/TheLayout
)作为内容和顶部布局之间的包装器:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="100dip"
android:paddingRight="100dip">
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/TheLayout"
android:layout_gravity="right">
<TextView android:id="@+id/TextView01"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1"
android:text="this is my text."
android:layout_gravity="center_vertical">
</TextView>
<ImageView android:id="@+id/ImageView01"
android:layout_height="wrap_content"
android:background="@drawable/icon"
android:layout_width="fill_parent">
</ImageView>
</LinearLayout>
</LinearLayout>
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMaxSpec = MeasureSpec.makeMeasureSpec(270 /*pixels*/, MeasureSpec.AT_MOST);
super.onMeasure(widthMaxSpec, heightMeasureSpec);
}