layout-maxWidth属性用法

 

对于maxWidth属性,相信大家都不陌生。不过,今天我遇到了一个问题,就是当我希望一个relayout的宽度有个最大值的时候,用maxWidth却没办法实现。这里总结下maxWidth 的用法

1.直接作用在控件上(textview为例)

代码如下

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.test1.MainActivity" >

    <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:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
        
            android:text="dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
            android:textSize="20dp" />
    </LinearLayout>

</RelativeLayout>

结果如下

2)我们给textview加入maxwidth

        <TextView
            android:maxWidth="100dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:text="dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
            android:textSize="20dp" />

结果如下

可以看到,这个属性发挥了效果,成功限制了textview的最大宽度

3)如果我们把这个属性加载到它的父容器linearlayout中

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxWidth="100dp"
        android:layout_centerInParent="true" >

结果如下

可以看到,它是没有效果的。如果你改为

android:layout_maxWidth 会报错

4)现在我们尝试下一个新的属性xmlns:internal="http://schemas.android.com/apk/prv/res/android"

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="300dp"
        android:layout_centerInParent="true"
        internal:layout_maxHeight="20dp"
        internal:layout_maxWidth="100dp"
        internal:layout_minHeight="20dp" >

结果还是没有效果。

还是不知道到底有没有什么方法可以设置一个layout的最大宽度

posted @ 2015-05-22 18:02  小白屋  阅读(6420)  评论(0编辑  收藏  举报