Android 设置LinearLayout的最大宽度
如何设置水平Android 设置LinearLayout的最大宽度,android,user-interface,layout,Android,User Interface,Layout,如何设置水平线性布局的最大宽度?因此,如果内容较短(例如,一些文本),则布局会缩小,如果内容较长,则其扩展不会超过某个最大宽度值 我更喜欢在XML级别这样做。这样做了-我需要添加另一个布局(@+id/TheLayout)作为内容和顶部布局之间的包装: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" and
线性布局的最大宽度?因此,如果内容较短(例如,一些文本),则布局会缩小,如果内容较长,则其扩展不会超过某个最大宽度值
我更喜欢在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>
有关使用ViewGroup子类的另一种方法,请参见我的答案欢迎来到这里,您最好解释一下为什么您的代码可以解决他的问题,谢谢。很好!工作得很有魅力。
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMaxSpec = MeasureSpec.makeMeasureSpec(270 /*pixels*/, MeasureSpec.AT_MOST);
super.onMeasure(widthMaxSpec, heightMeasureSpec);
}