代码之家  ›  专栏  ›  技术社区  ›  Asahi

设置线性布局的最大宽度

  •  8
  • Asahi  · 技术社区  · 14 年前

    如何设置水平面的最大宽度 LinearLayout ?因此,如果内容较短(例如某些文本),布局将缩小,如果内容较长,则展开的宽度不会超过某个最大宽度值。

    我更喜欢在XML级别执行此操作。

    2 回复  |  直到 14 年前
        1
  •  7
  •   Community CDub    8 年前

    这就是我需要的 beyond what was suggested in prior answer 是添加其他布局( @+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>
    
        2
  •  0
  •   Bill    9 年前
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMaxSpec = MeasureSpec.makeMeasureSpec(270 /*pixels*/, MeasureSpec.AT_MOST);
        super.onMeasure(widthMaxSpec, heightMeasureSpec);
    }