【问题标题】:button looks different on different android devices or versions按钮在不同的 android 设备或版本上看起来不同
【发布时间】:2014-04-06 09:59:48
【问题描述】:

我有两个简单的按钮,想给他们一个自定义的外观,所以我只是给按钮一个背景颜色。这在我的 4.2 上运行的 Htc One 上看起来不错,但是当我在旧的 Lg P925 上测试它时,按钮高度只是包裹了它的内容(就像默认填充消失了一样)。

像这样:

所以我做了一些进一步的研究,发现我必须使用能够为按钮添加功能的状态列表绘制。

所以我这样做了:

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>

按钮聚焦

    <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>

按下按钮

    <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>  

按钮正常

    <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 name="button" parent="@android:style/Widget.Button">
  <item name="android:background">@drawable/button</item>
  </style>

然后我将样式添加到按钮:

           <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"
             />

这是我得到的:

在我的 HTC(运行 4.2)上

这里的按钮更大,因为它将 5dp 顶部和底部填充(来自我的状态 drawables xml)添加到按钮的默认系统填充。

在我运行 2.3 的 Lg P925 上,我得到了这个:

按钮比我的 htc 上的小。似乎当我向按钮添加背景时,它摆脱了默认填充。我只是不知道。

如何创建一个在所有设备上看起来都一样的按钮?

【问题讨论】:

    标签: android android-view android-xml android-button


    【解决方案1】:

    *文本字体因设备而异,因此请使用字体类来设置按钮的字体 *

    喜欢这个

    在 assets 文件夹中创建文件夹字体并粘贴 abc.ttf 文件。基本上 .ttf 文件是一个包含字体样式的文件,你不能免费下载它 .

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

    或者你可以像这样使用android的预定义字体

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

    sans,monospace,serif 是预定义的字体

    享受编码

    会有用的

    【讨论】:

      【解决方案2】:

      我相信关键是这句话:

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

      请注意,您正在扩展父样式,这可能会因 Android 版本等而异。请尝试不使用父位。

      【讨论】:

        猜你喜欢
        • 2016-07-26
        • 1970-01-01
        • 2017-04-11
        • 1970-01-01
        • 2014-10-30
        • 2020-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多