2014-03-07 25 views
0

此問題與使用屏幕寬度擴展元素有關,但僅限於最大寬度。展開屏幕尺寸最大爲maxWidth的TextView

我的場景包含一個垂直定向的LinearLayout,其中包含一個固定的行序列。這些行中的每一行都是一個水平的LinearLayout,它包含一些由TextView元素分隔的EditText元素。 TextView元素的文本包含一個數學符號(例如+或 - 或/),用於指示第二個框的內容將被添加到(或從等中減去)第一個框,以顯示在UI中的其他位置。

EditText元素必須保持它們當前的固定寬度,但我希望TextView元素(像分隔符一樣)在寬度上擴展(如果屏幕允許),但只能達到30dp的最大寬度。目前的一個例子列布局的樣子:

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:paddingLeft="@dimen/row_side_padding" 
    android:paddingRight="@dimen/row_side_padding" 
    android:paddingTop="5dp" 
    android:paddingBottom="5dp"> 

    <EditText 
     android:id="@+id/editTextBoxOne" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:hint="@string/box_one_hint" 
     android:imeOptions="actionDone" 
     android:inputType="numberSigned" 
     android:maxLines="1" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text="@string/plus" /> 

    <EditText 
     android:id="@+id/editTextBoxTwo" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:hint="@string/box_two_hint" 
     android:imeOptions="actionDone" 
     android:inputType="numberSigned" 
     android:maxLines="1" /> 
</LinearLayout> 

這個當前的代碼包裝了的TextView的文本(只是在這個例子中一個加號)的寬度的寬度。這正是我在小屏幕上想要的!

但是,在更寬的屏幕上,如果TextView變得更寬以將元素間隔一點,那將會很不錯。我能夠通過將TextView設置爲:

<TextView 
    android:layout_width="match_parent" 
    android:layout_weight="1" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:text="@string/plus" /> 

但是,這使得TextView太大。如果屏幕寬度允許,我希望它擴展到最大30dp。但是from what I understand,android:maxWidth不適用於android:layout_width="match_parent"因此任何maxWidth都被忽略。

那麼如何擴展我的TextView基於屏幕寬度,但只能達到最大寬度,而留下我的EditText元素相同的大小?

回答

0

android:layout_width設置爲0dip ...這將幫助TextView獲取尺寸爲EditTexts之後可用的空間。

<TextView 
    android:layout_width="0dip" 
    android:layout_weight="1" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:text="@string/plus" /> 
0

對於那些誰是解決同樣的問題...

它似乎並不彷彿有辦法做到這一點。相反,最好的路線是創建一些與某些屏幕寬度相關聯的獨立佈局文件(即,在res/layout-w600dp以及res/layout中捕獲最小的屏幕寬度)。我指定一個單一的TextView在我layout_plus.xml文件:

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingTop="10dp" 
    android:paddingLeft="5dp" 
    android:paddingRight="5dp" 
    android:gravity="center" 
    android:text="@string/plus" > 
</TextView> 

對於每個I使用不同量paddingLeft和paddingRight的這些屏幕寬度依賴佈局,手動執行的周圍的TextView空間的擴大。如果你願意,你可以改變TextView的寬度。

然後在您的主佈局中,刪除您試圖展開的TextView並使用 tag。這將允許您的屏幕寬度相關文件被自動拖入 - 無論哪一個都適合。我的包含如下所示:

<include 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    layout="@layout/layout_plus"/> 

該文檔聲明您應該覆蓋layout_width和layout_height屬性。

我想覆蓋其他屬性,例如覆蓋文本以包含減號而不是加號(允許我更多地重複使用)。不幸的是,include不會讓你重寫其他屬性,所以我必須爲此創建一組layout_minus.xml佈局。

作爲使用修改佈局的替代方法,可以選擇僅創建由TextView顯示的字符串資源的屏幕寬度相關版本,您可以在其中爲較大寬度版本插入空格。這需要更少的更改,並且可能更少的新文件,但字符串內部的硬編碼空間使得稍後難以調整和調整。