开发者

Strange 9-patch behaviour

开发者 https://www.devze.com 2023-03-20 22:14 出处:网络
I\'ve come across an issue with a 9-patch button background and I can\'t figure out what I\'m doing wrong.

I've come across an issue with a 9-patch button background and I can't figure out what I'm doing wrong.

This is the xml configuration for the button:

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

In this config, I'm using a selector so that the background is different when the button is pressed (the same 9-patch, only in greyscale). I've also tried using the nine-patch directly, without the selector, and it appears to make no difference.

This is the 9-patch image:

Strange 9-patch behaviour

And this is the result:

Strange 9-patch behaviour

As you can see, the padding and stretching areas appear to be completely ignored. I have the impression that it's using the 9-patch image as a plain png, but I can't figure out why. Any help would be appreciated because this is starting to get o开发者_StackOverflow社区n my nerves :p

Thanks in advance.

Edit

Ok so this is the full layout in which I'm using the button.

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

And code-wise, there isn't much I can show you. All I do is add a very simple listener to the button:

Button back = (Button) findViewById(R.id.buttonBack);
    back.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            finish();
        }
    });


It looks like the button actually is that size, and the background + size of the containing RelativeLayout makes you think otherwise. You don't actually need to use the button widget. You can set the background of the RelativeLayout to your button's background and implement an OnClickListener. Add a TextView and ImageView to it to get your desired look.


Both your button and your imageview have layout_width set to WRAP_CONTENT, so they won't be larger than the original size and thus your 9-patch won't be stretched.

0

精彩评论

暂无评论...
验证码 换一张
取 消