简体   繁体   English

Android按钮大小在不同设备上更改(宽度设置为dp)

[英]Android button size changes at different devices (width is set to dp)

I am designing an app which needs the customized button. 我正在设计一个需要自定义按钮的应用程序。 I made it by inheriting the Android Button and I set its width to 300dp in the layout xml. 我继承了Android Button,并在布局xml中将其宽度设置为300dp。 However, I found the size of button is changing on different devices. 但是,我发现按钮的大小在不同的设备上正在变化。 Here are the example: 这是示例:

My UI in the original device : Galaxy S5 我在原始设备中的用户界面: Galaxy S5

My UI in the new device : Nexus 5X 我在新设备上的用户界面: Nexus 5X

My UI in another device : Galaxy Note4 (In this trial, I set the left button to use sp instead of dp) 我在其他设备上的用户界面: Galaxy Note4 (在本次试用中,我将左侧按钮设置为使用sp而不是dp)

It seems the whole layout scale is smaller in my new device even though both of them are 1080 x 1920 pixels. 看来我的新设备的整体布局比例都较小,即使它们都是1080 x 1920像素。

I also try to use sp but it behaves the same (problem). 我也尝试使用sp,但是它的行为相同(问题)。 My guess is the style? 我的猜测是风格? Can any one gives me more thoughts. 谁能给我更多的想法。 Thanks! 谢谢!

--- UPDATE --- Hi, let me rephrase my problem. ---更新---嗨,我改一下我的问题。

I am looking for a way to make a button which width is fix to like (relatively) 1/3 screen width at any device . 我正在寻找一种方法来制作宽度固定为(相对) 任何设备上1/3屏幕宽度的按钮。 And its text also changes accordingly. 并且其文本也相应地更改。 I think the ultimate way is to translate everything to pixel and assign those value manually in program. 我认为最终的方法是将所有内容转换为像素,并在程序中手动分配这些值。 I used to think using dp and sp is an easy alternative to achieve it, but it turns out not :( 我曾经认为使用dp和sp是实现此目的的简便方法,但事实并非如此:(

If you are using dp then it will change according to device's screen density. 如果您使用的是dp,则它将根据设备的屏幕密度而变化。 In order to keep the button size same in different screen density conditions you need to use pt , in or mm in your button's width. 为了在不同的屏幕密度条件下保持按钮大小不变,您需要在按钮的宽度中使用ptinmm

Please check link : What is the difference between "px", "dp", "dip" and "sp" on Android? 请检查链接: Android上的“ px”,“ dp”,“ dip”和“ sp”有什么区别?

To set width of each child programmaticaly, 要以编程方式设置每个孩子的宽度,

view.getLayoutParams().width = getScreenWidth() / VIEWS_COUNT_TO_DISPLAY;

And Screensize as below 和屏幕尺寸如下

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics).widthPixels;

Now, If you want to set Text than use SP for that. 现在,如果要设置文本而不是使用SP

I think here is what you want. 我想这就是你想要的。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center_horizontal"
    android:weightSum="3">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="1">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleType="fitXY"
            android:src="@drawable/test"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Normal Charge (Testing)"/>

     </LinearLayout>
</LinearLayout>

With the use of weightSum and layout_weight, you can fix the item as 1/3 width of your screen. 通过使用weightSum和layout_weight,可以将项目固定为屏幕的1/3宽度。

Inside the imageview, android:scaleType="fitXY" 在imageview内部,android:scaleType =“ fitXY”
It means that the image will be changed to size in order to match the size of imageview. 这意味着将更改图像的大小以匹配imageview的大小。
You can delete that attribute if you do not want that. 如果不需要,可以删除该属性。

For the image, the best way is to prepare different size of the image inside different folders: 对于图像,最好的方法是在不同的文件夹中准备不同大小的图像:
for mdpi it should be 100X100 对于mdpi,应为100X100
for ldpi it should be 75X75 对于ldpi,应为75X75
for hdpi it should be 150X150 (eg Xperia U) 对于hdpi,应为150X150(例如Xperia U)
for xhdpi it should be 200X200 (eg Galaxy S3, Galaxy Note II, Xperia S) 对于xhdpi,应为200X200(例如Galaxy S3,Galaxy Note II,Xperia S)
for xxhdpi it should be 300X300 (eg Galaxy S4, Galaxy S5, Xperia Z, Xperia Z1, Xperia Z2) 对于xxhdpi,应为300X300(例如Galaxy S4,Galaxy S5,Xperia Z,Xperia Z1,Xperia Z2)

The above size is just an example, you have to find the base case, i can explain more about that if you need. 上面的大小仅是示例,您必须找到基本情况,如果需要,我可以详细说明。
Hop that can help, thanks! 希望对您有所帮助,谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM