简体   繁体   English

按钮在不同的Android设备或版本上看起来有所不同

[英]button looks different on different android devices or versions

I have two simple buttons and wanted to give them a custom look, so i just gave the button a background color. 我有两个简单的按钮,想给他们一个自定义的外观,所以我只是给按钮提供了背景色。 This looked fine on my Htc One which runs on 4.2, but when i tested it on an old Lg P925, the buttons height just wrapped its content (just as if the default padding was gone). 这在运行4.2的Htc One上看起来不错,但是当我在旧的Lg P925上测试它时,按钮的高度刚好包裹了它的内容(就像默认的填充消失了一样)。

Like this: 像这样:

在此处输入图片说明

So i did some further research and found out that i had to use a state list draw able to add features to the button. 因此,我做了一些进一步的研究,发现我必须使用状态列表绘制功能才能向按钮添加功能。

So i did that like this: 所以我这样做是这样的:

Button.xml Button.xml

 <selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="@drawable/button_pressed" />
<item android:state_focused="true" android:drawable="@drawable/button_focused" />
<item android:drawable="@drawable/button_normal" />

 </selector>

Button focused 按钮集中

    <shape xmlns:android="http://schemas.android.com/apk/res/android"   
    android:shape="rectangle">
    <solid android:color="@color/greybutton"/>
    <corners android:radius="0dp" />
    <stroke android:color="@color/greybutton" android:width="2dp" />
    <padding android:top="5dp" android:bottom="5dp"             
    android:left="10dp"android:right="10dp" />
    </shape>

Button pressed 按下按钮

    <shape xmlns:android="http://schemas.android.com/apk/res/android"      
    android:shape="rectangle">
    <solid android:color="@color/greybutton"/>
    <corners android:radius="0dp" />
    <stroke android:color="@color/greybutton" android:width="2dp" />
    <padding android:top="5dp" android:bottom="5dp" android:left="10dp"     
    android:right="10dp" />
    </shape>  

Button normal 按键正常

    <shape xmlns:android="http://schemas.android.com/apk/res/android"  
    android:shape="rectangle">
    <gradient android:startColor="#FFFFFFFF" android:endColor="#A4A4A4"  
    android:angle="270" />
    <solid android:color="@color/darkerGrey"/>
    <corners android:radius="0dp" />
    <padding android:top="5dp" android:bottom="5dp" android:left="10dp"   
    android:right="10dp"/>
    </shape>

Style 样式

  <style name="button" parent="@android:style/Widget.Button">
  <item name="android:background">@drawable/button</item>
  </style>

Then i added the style to the button: 然后我将样式添加到按钮:

           <Button
            android:id="@+id/SignUpBtn"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:text="@string/CreateAcct"
            android:textColor="@color/white"
            style="@style/button"
            android:textSize="17sp"
             />

This is what i got: 这就是我得到的:

On my HTC(Running 4.2) 在我的HTC上(运行4.2)

Here the buttons are bigger because it adds the 5dp top and bottom padding(from my state drawables xml) to the default system padding of the button. 这里的按钮更大,因为它在按钮的默认系统填充中添加了5dp顶部和底部填充(来自我的state drawables xml)。

On my Lg P925 running on 2.3 i get this: 在运行于2.3的Lg P925上,我得到以下信息:

在此处输入图片说明

The buttons are smaller than those on my htc. 按钮比我的htc上的按钮小。 It seems that when i add a background to the button it gets rid of the default padding. 看来,当我向按钮添加背景时,它将摆脱默认的填充。 I just don't know. 我只是不知道

How can i create a button that would look the same all devices? 如何创建在所有设备上看起来都一样的按钮?

*Text font varies from device to device so use typeface class to set font typeface of button * *文本字体因设备而异,因此请使用字体类设置按钮的字体*

like this 像这样

create a folder font in assets folder and paste abc.ttf file . 在Assets文件夹中创建文件夹字体,然后粘贴abc.ttf文件。 basically .ttf file is a file that contains font style u cant download it free . 基本上.ttf文件是包含免费字体的文件

Button txt = (Button) findViewById(R.id.custom_font);  
Typeface font = Typeface.createFromAsset(getAssets(), "abc.TTF");  
txt.setTypeface(font);

or u can use use predefined fonts of android like this 或者你可以使用这样的使用Android的预定义字体

<Button 
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:typeface="sans"/>

sans,monospace,serif are predefined fonts sans,monospace,serif是预定义的字体

enjoy coding 享受编码

it will work 它会工作

I believe the key is this statement: 我相信关键是这样的声明:

<style name="button" parent="@android:style/Widget.Button">
<item name="android:background">@drawable/button</item>

</style>

Note that you are extending the parent style, which might vary depending on the Android version, etc. Try it without the parent bit. 请注意,您正在扩展父样式,该样式可能会因Android版本等而异。请在没有父样式的情况下进行尝试。

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

相关问题 Android按钮样式在不同设备上看起来不同(AOSP和三星Galaxy) - Android button styling looks different on different devices (AOSP and Samsung Galaxy) 在不同版本的Android上,TimePicker看起来有所不同 - TimePicker looks different on different versions of Android Android:布局在不同设备上看起来有所不同 - Android: Layout looks different on different devices Android 布局在不同设备上看起来不同 - Android layout looks different on different devices 设备上的布局看起来有所不同 - Layout looks different on devices dp中的高度和宽度在不同的设备上看起来有所不同(Android) - Height and width in dp looks different on different devices (Android) 为什么在不同的Android设备上相同的颜色看起来不同 - why the same color looks different on different android devices Android:使用 PrintedPdfDocument 创建的 PDF 在不同的设备上看起来不同 - Android: PDF created using PrintedPdfDocument looks different on different devices Google Maps与其他Android版本/设备的兼容性 - Google Maps compatibility with different Android versions/devices Android,不同设备上的按钮大小 - Android, button sizes on different devices
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM