为LinearLayout设置最大宽度

10

我该如何设置水平 LinearLayout 的最大宽度?如果内容很短(比如一些文本),布局会缩小,如果内容较长,则不会超过某个最大宽度值。

我更喜欢在 XML 层面上进行这个操作。


请看我在这里的答案,使用ViewGroup子类的另一种方法。 - Dori
2个回答

8

这就是我需要的——在之前的答案中所建议的基础上,进一步添加另一个布局(@+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>

不错!运行得很顺利。 - Ready Android

1
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int widthMaxSpec = MeasureSpec.makeMeasureSpec(270 /*pixels*/, MeasureSpec.AT_MOST);
    super.onMeasure(widthMaxSpec, heightMeasureSpec);
}

欢迎来到SO,您最好解释一下为什么您的代码可以解决他的问题,谢谢。 - Muhammed Refaat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接