[英]How to change button size according to screen's device?
I have a button with size width=216
and height=110
in a device screen resolution is 1080 x 1920. When I change to other device (smaller resolution), the button is so big. 我在设备屏幕分辨率中有一个width=216
, height=110
的按钮,屏幕分辨率为1080 x1920。当我切换到其他设备(分辨率较低)时,该按钮太大。 Hence, I need to write a fuction to dynamically change button size according to screen's device resolution. 因此,我需要编写一个函数来根据屏幕的设备分辨率动态更改按钮的大小。 First, I will check target device resolution then I change it based on above my current device. 首先,我将检查目标设备的分辨率,然后根据当前设备的上方进行更改。 However, the button resolution is not correct. 但是,按钮分辨率不正确。 How can I fix it? 我该如何解决? This is my code 这是我的代码
My code in the 1080x1920 我在1080x1920中的代码
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
>
<Button
android:id="@+id/btn"
android:layout_width="216dp"
android:layout_height="110dp"
android:layout_gravity="center"
android:scaleType="fitCenter"
/>
</LinearLayout>
Changed to dynamic way: 更改为动态方式:
public int WIDTH ;
public int HEIGHT ;
//Get screen resolution
DisplayMetrics metrics = getResources().getDisplayMetrics();
HEIGHT = metrics.heightPixels;
WIDTH= metrics.widthPixels;
int height_px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 110, getResources().getDisplayMetrics());
int weight_px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 216, getResources().getDisplayMetrics());
int width_linearlayout = Math.round((float)weight_px/1080*WIDTH);
int height_linearlayout = Math.round((float)height_px/1920*HEIGHT);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width_linearlayout,height_linearlayout);
btn.setLayoutParams(layoutParams);
What I would do in situations like this is create a specific dimens.xml for different screen size requirements. 在这种情况下,我将为不同的屏幕尺寸要求创建一个特定的dimens.xml。
Each of these would contain parameters specific to devices with screens fitting those dimensions. 其中每个参数都包含特定于带有适合这些尺寸的屏幕的设备的参数。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="nav_header_height">160dp</dimen>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="button_size_height">32dp</dimen>
<dimen name="button_size_width">70dp</dimen>
</resources>
No Extra coding, just need to maintain multiple xml files containing your button parameters. 无需额外的编码,只需维护包含您的按钮参数的多个xml文件。
Each dimension would be referenced as such: 每个维度都将这样引用:
<Button
android:id="@+id/btn"
android:layout_width="@dimen/button_size_width"
android:layout_height="@dimen/button_size_height"
android:layout_gravity="center"
android:scaleType="fitCenter"
/>
You have to use layout_weight in order to achieve what you want, and that will be used by wrapping each row of buttons inside a LinearLayout and all that LinearLayouts plus the TextView will be wrapped inside one big LinearLayout instead of your RelativeLayout like the following: 您必须使用layout_weight来实现所需的功能,并且通过将每行按钮包装在LinearLayout中来使用,而所有LinearLayouts和TextView都将包装在一个大的LinearLayout中,而不是如下所示的RelativeLayout中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff000000"
android:orientation="vertical"
android:weightSum="6" >
<TextView
android:id="@+id/disp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:inputType="none"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@android:color/white"
android:textSize="32sp"
android:textStyle="normal" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="4" >
<Button
android:id="@+id/clear"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:background="@drawable/clear_btn" />
<!-- then the three remaining buttons -->
<LinearLayout />
<!-- then the four remaining rows -->
<!-- in the last row the "0" button will have layout_weight="2" NOT "1" -->
</LinearLayout>
Add folder values-sw540dp
its is for 1080p screens . 添加文件夹values-sw540dp
它是用于1080p屏幕的 。 In this folder add dimens.xml
在此文件夹中添加dimens.xml
<dimen name="btn_height">100dp</dimen>
<dimen name="btn_width">65dp</dimen>
Edit your xml 编辑您的xml
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
>
<Button
android:id="@+id/btn"
android:layout_width="@dimen/btn_width"
android:layout_height="@dimen/btn_height"
android:layout_gravity="center"
android:scaleType="fitCenter"
/>
</LinearLayout>
Other resolutions 其他决议
values-sw720dp 10.1” tablet 1280x800 mdpi
values-sw600dp 7.0” tablet 1024x600 mdpi
values-sw480dp 5.4” 480x854 mdpi
values-sw480dp 5.1” 480x800 mdpi
values-xhdpi 4.7” 1280x720 xhdpi
values-xhdpi 4.65” 720x1280 xhdpi
values-hdpi 4.0” 480x800 hdpi
values-hdpi 3.7” 480x854 hdpi
values-mdpi 3.2” 320x480 mdpi
values-ldpi 3.4” 240x432 ldpi
values-ldpi 3.3” 240x400 ldpi
values-ldpi 2.7” 240x320 ldpi
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.