【发布时间】:2011-07-18 16:21:48
【问题描述】:
我遇到了一个 9 补丁按钮背景的问题,我不知道我做错了什么。
这是按钮的xml配置:
<Button android:layout_width="wrap_content" android:id="@+id/buttonBack"
android:layout_alignParentLeft="true" android:layout_centerVertical="true"
android:padding="2dp" android:layout_height="wrap_content"
android:background="@drawable/selector_bg_back_button" android:text="@string/back"
android:textColor="#ffffff" android:textStyle="bold"
android:layout_marginLeft="2dp" />
在这个配置中,我使用了一个选择器,以便在按下按钮时背景会有所不同(相同的 9-patch,只有灰度)。我也试过直接用九补丁,不用选择器,好像没什么区别。
这是 9 补丁图像:
结果如下:
如您所见,填充和拉伸区域似乎完全被忽略了。 我的印象是它使用 9-patch 图像作为普通 png,但我不知道为什么。任何帮助将不胜感激,因为这开始让我感到不安:p
提前致谢。
编辑
好的,这就是我使用按钮的完整布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<RelativeLayout android:layout_width="fill_parent"
android:background="#0ab0ed" android:layout_height="40dip"
android:padding="0px">
<Button android:layout_width="wrap_content" android:id="@+id/buttonBack"
android:layout_alignParentLeft="true" android:layout_centerVertical="true"
android:padding="2dp" android:layout_height="wrap_content"
android:background="@drawable/btn_bg_back_blue" android:text="@string/back"
android:textColor="#ffffff" android:textStyle="bold"
android:layout_marginLeft="2dp" />
<ImageView android:layout_width="wrap_content"
android:layout_height="fill_parent" android:src="@drawable/logo"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<RelativeLayout android:layout_width="fill_parent"
android:padding="10dp" android:layout_height="wrap_content"
android:background="#f5f5f5">
<ImageView android:layout_height="wrap_content"
android:layout_margin="10dp" android:layout_centerHorizontal="true"
android:layout_width="wrap_content" android:id="@+id/image" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/txtTitle"
android:textColor="#182973" android:textSize="16dp"
android:textStyle="bold" android:gravity="center_horizontal" />
</RelativeLayout>
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:background="#ffffff">
<TextView android:layout_width="wrap_content"
android:layout_alignParentTop="true" android:layout_alignParentLeft="true"
android:layout_height="wrap_content" android:id="@+id/txtDate" style="@style/content" />
<ScrollView android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</ScrollView>
</RelativeLayout>
就代码而言,我无法向您展示太多内容。我所做的只是为按钮添加一个非常简单的监听器:
Button back = (Button) findViewById(R.id.buttonBack);
back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
【问题讨论】:
-
文件名是否使用“.9.png”后缀?
-
把它放到一个项目中。工作得很好!看起来问题可能出在其他地方,您能否公开更多代码?
-
我编辑了帖子以包含完整的布局和我使用此按钮的代码。正如你所看到的,那里并没有发生太多事情,这也是我觉得它无法正常工作的部分原因......我真的没有做任何特别的事情。
-
您是否尝试过进行干净的>构建?无关:为什么要在 Android 设备上实现后退按钮?
标签: android button background selector nine-patch