[英]android change button size according to device screen size
我想將我的應用程序設計調整為適用於所有android設備,我已經嘗試過按百分比設置元素大小屬性,但是這種方式讓我感到困惑,因此我嘗試了以下代碼:(通過設備屏幕大小設置寬度和高度/ 5)
public void fixButtonSizes (){
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width1 = size.x;
int height1 = size.y;
Button button1vid = (Button) findViewById(R.id.button1);
button1vid.setHeight(height1 / 5);
button1vid.setWidth(width1 / 5);
}
而且它對我也不起作用
要在android中使用寬度或高度百分比,您應該將元素包裝在LinearLayout
並使用元素的layout_weight
屬性。 另外,您還必須根據需要的尺寸將0dp
放入layout_width
或layout_width
。
要計算元素將具有的高度或寬度,必須對布局中元素的所有layout_weight求和,然后除以元素的權重。
例如,如果要水平創建5個寬度相同的按鈕,可以使用以下按鈕:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 1"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 2"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 3"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 4"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 5"/>
</LinearLayout>
我希望這能使您澄清。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.