在模拟器和Android设备中改变输出

时间:2016-07-02 09:30:12

标签: android xml android-layout

我正在使用Android Studio显示圆形按钮。这是代码(activity_main.xml),

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.hp.tmp.MainActivity">

<Button
    android:id="@+id/angry_btn"

    android:text="Click"
    android:textColor="#000000"
    android:textSize="15sp"

    android:layout_width="95dp"
    android:layout_height="95dp"
    android:background="@drawable/button"
    android:backgroundTint="#ed8181" />
</RelativeLayout>

Drawable

中的button.xml
    <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <corners
        android:radius="100dp"
        />
    <padding
        android:left="0dp"
        android:top="0dp"
        android:right="0dp"
        android:bottom="0dp"
        />
    <size
        android:width="100dp"
        android:height="100dp"
        />
</shape>

以下是仿真器上的输出(预期)(Nexus 5 API 23) expected

这是Android设备(HTC欲望616)与Android版本4.2.2的输出 actual

我已将项目的最小sdk设置为14。 非常感谢帮助。 感谢您宝贵的时间。

1 个答案:

答案 0 :(得分:0)

你的问题是android:backgroundTint="#ed8181"是API级别21而你的HTC欲望616的Api等级为19。

以下列方式更改您的代码并且它可以正常工作。

布局

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <Button
        android:id="@+id/angry_btn"

        android:text="Click"
        android:textColor="#000000"
        android:textSize="15sp"

        android:layout_width="95dp"
        android:layout_height="95dp"
        android:background="@drawable/button"
         />
</RelativeLayout>

资源文件

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="#ed8181" />
    <corners android:radius="100dp" />
    <padding
        android:bottom="0dp"
        android:left="0dp"
        android:right="0dp"
        android:top="0dp" />
    <size
        android:width="100dp"
        android:height="100dp" />
</shape>