2014-09-25 33 views
0

這是我目前的佈局。如何讓文本視圖比我的佈局中的按鈕佔用更多空間?

enter image description here

而且我目前的佈局代碼

<?xml version="1.0" encoding="utf-8"?> 
<TableRow xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:background="@android:color/transparent" > 

<TextView 
    android:id="@+id/stockView" 
    android:text="TextView" 
    android:layout_weight="1" /> 

<Button 
    android:id="@+id/stockQuote" 
    style="?android:attr/buttonStyleSmall" 
    android:text="@string/get_stock_quote" 
     android:layout_weight="1" /> 

<Button 
    android:id="@+id/webStock" 
    style="?android:attr/buttonStyleSmall" 
    android:text="@string/go_to_website" 
     android:layout_weight="1" /> 

</TableRow> 

一切工作了罰款,我的理由是,因爲我給每個元素的「1」 layout_width,它們佔用的空間相同數量在表格行中。不過,我希望文本視圖佔用比按鈕更多的空間,所以我將文本視圖的layout_weight更改爲2,以便在每個按鈕佔用1/4按鈕時佔用額外空間的一半。我做了這個改變之後,這就是我的佈局。 enter image description here

有沒有人明白爲什麼會發生這種情況?文本視圖現在甚至不可見。我該如何解決這個問題,讓文本視圖佔用更多的空間?

+0

當你說「layout_ * width *」時,你的意思是「layout_ * weight *」嗎? – aij 2014-09-25 20:13:12

+0

你可以將'TableRow'的'weightSum'設置爲'4',然後使用你想要的值'(2,1,1)'。您還可以使用'layout_weight'的浮點值來獲取更精確的大小關係。 – nem035 2014-09-25 20:14:30

+0

之前的重量總和是多少?這是僅與TableRows? – committedandroider 2014-09-25 21:38:42

回答

1

weightSum定義最大權重和。如果未指定,則通過添加所有孩子的layout_weight來計算總和。

<TableRow android:weightSum="1"> 
<TextView 
    android:id="@+id/stockView" 
    android:text="TextView" 
    android:layout_weight="0.5" /> 

<Button 
    android:id="@+id/stockQuote" 
    style="?android:attr/buttonStyleSmall" 
    android:text="@string/get_stock_quote" 
     android:layout_weight="0.25" /> 

<Button 
    android:id="@+id/webStock" 
    style="?android:attr/buttonStyleSmall" 
    android:text="@string/go_to_website" 
     android:layout_weight="0.25" /> 
</TableRow> 

你可以閱讀更多關於它here

+0

感謝這項工作 – committedandroider 2014-09-25 21:41:32

+0

事實證明,代碼工作正常。預覽剛剛關閉了一點 – committedandroider 2014-09-25 21:43:23

1

weightSum = TOTAL(100%)

layout_weight =%

試試這個:

<?xml version="1.0" encoding="utf-8"?> 
<TableRow xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:background="@android:color/transparent" 
android:weightSum="100"> 

<TextView 
    android:id="@+id/stockView" 
    android:text="TextView" 
    android:layout_weight="40" /> 

<Button 
    android:id="@+id/stockQuote" 
    style="?android:attr/buttonStyleSmall" 
    android:text="@string/get_stock_quote" 
     android:layout_weight="30" /> 

<Button 
    android:id="@+id/webStock" 
    style="?android:attr/buttonStyleSmall" 
    android:text="@string/go_to_website" 
     android:layout_weight="30" /> 

</TableRow> 
0

你即將給出你需要做的總分區比例

例如:如果你想在屏幕的寬度分成3,那麼weightSum應該是3和分流比,你應該給每個視圖單獨

下面是示例代碼,請試試這個,

<?xml version="1.0" encoding="utf-8"?> 
<TableRow xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:background="@android:color/transparent" 
    android:weightSum="1" > 

    <TextView 
     android:id="@+id/stockView" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:text="TextView" 
     android:layout_weight="0.5" /> 

    <Button 
     android:id="@+id/stockQuote" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content"    
     style="?android:attr/buttonStyleSmall" 
     android:text="@string/get_stock_quote" 
     android:layout_weight="0.25" /> 

    <Button 
     android:id="@+id/webStock" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content"    
     style="?android:attr/buttonStyleSmall" 
     android:text="@string/go_to_website" 
     android:layout_weight="0.25" /> 

</TableRow> 
相關問題