android - 具有完全相同代码的按钮,产生两种不同的样式

标签 android android-layout listview android-button

我有两组不同的按钮,一组在 Activity 中,一组在 ListView 中。它们具有非常相似的代码,但显示两种不同的样式。

标题按钮组代码:

 <Button
        android:id="@+id/invoices_1year"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="0.2"
        android:onClick="refresh"
        android:text="@string/Invoices_1_year" />

而 Listview 的按钮代码是:

<Button
    android:id="@+id/invoiceButton"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_weight="0.25"
    android:text="View Pdf" />

@ir2pid 的 Listview 的 Xml:

<ListView
    android:id="@+id/InvoiceList"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/searchArea" />

我在我的设备上得到的输出:

Image of Whats displayed on device.

我的预期结果是两个按钮组看起来一样。

最佳答案

在drawable文件夹中创建round.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" >
        <shape android:shape="rectangle"  >
            <corners android:radius="3dip" />
            <stroke android:width="1dip" android:color="#5e7974" />
            <gradient android:angle="-90" android:startColor="#e4d46f" android:endColor="#e4d079"  />
        </shape>
    </item>
    <item android:state_focused="true">
        <shape android:shape="rectangle"  >
            <corners android:radius="3dip" />
            <stroke android:width="1dip" android:color="#c7cf2d" />
            <solid android:color="#e9da67"/>
        </shape>
    </item>
    <item >
        <shape android:shape="rectangle"  >
            <corners android:radius="3dip" />
            <stroke android:width="1dip" android:color="#e7d851" />
            <gradient android:angle="-90" android:startColor="#e4d079" android:endColor="#87cf8d" />
        </shape>
    </item>
</selector>

然后在按钮中添加

android:background="@drawable/round"

关于android - 具有完全相同代码的按钮,产生两种不同的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36466402/

相关文章:

java - 如何在 x 轴作为日期时间和 y 轴作为值之间绘制图表

android - 模仿 Gmail 应用标签列表

listview - 如何以简单的方式为UWP实现延迟加载ListView?

java - 如何在 RecyclerView 中设置 stackFromBottom?

android - 当我从android中的应用程序注销时如何禁用推送通知

java - Android - 条码 fragment 结果不显示

android - 布局检查器显示 ID 的位置

Android:如何制作android activity的翻转动画,就像iphone从左到右水平翻转?

C# ListView在List模式下调整Item宽度

javascript - 如何修改android & iOS项目,使入口文件成为react native中的普通js文件?