作爲Android應用程序的一部分,我構建了一個按鈕集。這些按鈕是LinearLayouts嵌套集的一部分。使用權重我已經根據包含的父LinearLayout的大小自動調整大小。這個想法是基於屏幕的像素數量和密度,將包含的佈局的大小設置爲多個像素;並讓按鈕設置根據該更改自行調整大小。Android:如何以編程方式設置佈局的大小
接下來的問題是:如何調整佈局大小。
我嘗試了幾種建議的技術,沒有接近工作。下面是建立按鈕組中XML的一個子集:
<LinearLayout android:layout_height="104pt" android:id="@+id/numberPadLayout" android:orientation="horizontal" android:layout_width="104pt"
android:background="#a0a0a0"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
>
<LinearLayout android:layout_weight="2" android:layout_height="fill_parent" android:id="@+id/linearLayout1" android:orientation="vertical" android:layout_width="wrap_content">
<Button android:text="1" android:id="@+id/button1" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
<Button android:text="4" android:id="@+id/button4" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
<Button android:text="7" android:id="@+id/button7" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
<Button android:text="-" android:id="@+id/buttonDash" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
</LinearLayout>
<LinearLayout android:layout_weight="2" android:layout_height="fill_parent" android:id="@+id/linearLayout2" android:orientation="vertical" android:layout_width="wrap_content">
<Button android:text="2" android:id="@+id/button2" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
<Button android:text="5" android:id="@+id/button5" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
<Button android:text="8" android:id="@+id/button8" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
<Button android:text="0" android:id="@+id/button0" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
</LinearLayout>
<LinearLayout android:layout_weight="2" android:layout_height="fill_parent" android:id="@+id/linearLayout3" android:orientation="vertical" android:layout_width="wrap_content">
<Button android:text="3" android:id="@+id/button3" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
<Button android:text="6" android:id="@+id/button6" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
<Button android:text="9" android:id="@+id/button9" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
<Button android:text="." android:id="@+id/buttonDot" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
</LinearLayout>
<LinearLayout android:layout_weight="2" android:layout_height="fill_parent" android:id="@+id/linearLayout4" android:orientation="vertical" android:layout_width="wrap_content">
<Button android:text="/" android:id="@+id/buttonBack" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
<Button android:text="\" android:id="@+id/buttonEnter" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
</LinearLayout>
</LinearLayout>
的兩個問題是:1)我如何才能從Java訪問numberPadLayout。一旦我有權訪問視圖,2)如何更改佈局的高度和寬度。
任何建議,將不勝感激。
謝謝你,那正是我所需要的。我似乎出錯了我的假設,我需要將參數應用回佈局才能工作。 – codingCat
如果您需要根據其他視圖的計算值進行更改,請將其與http://stackoverflow.com/a/8171014/9648結合使用。 – JohnnyLambada
它不起作用。我的佈局寬度是2000.當我打印'layout.getWidth()',它仍然是2000,即使我把'params.width = 1000' – 2015-03-24 09:13:30