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的最大寬度


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM