简体   繁体   中英

Android TextView Padding with a drawable background in xml

So... a known Android issue is that setting a drawable background to a TextView in conjunction with setting the padding (eg paddingLeft ) will only work if the background is set before the padding .
So far so good (and annoying).

But, what if I want to do it via .xml (and not programmatically)? :(
Any ideas?

Here is what I want:
左边填充应该大于drawable-to-text填充

But no matter what paddingLeft I set, nothing changes.
I'm positive it should work, but maybe it's a layout bug?

Here is the (very simplified) layout:

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button
    android:id="@+id/call"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:background="@drawable/call_btn"
    android:paddingLeft="@dimen/DESIRED_LEFT_PADDING"
    android:paddingStart="@dimen/DESIRED_LEFT_PADDING"
    android:drawableLeft="@drawable/ic_phone"
    android:drawablePadding="@dimen/drawable_padding"
    android:drawableStart="@drawable/ic_phone"
    android:singleLine="true" />

<Button
    android:id="@+id/address"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/call"
    android:paddingLeft="@dimen/rest_item_btn_side_margin"
    android:paddingStart="@dimen/rest_item_btn_side_margin"
    android:background="@drawable/navigate_btn"
    android:drawableLeft="@drawable/ic_address"
    android:drawablePadding="@dimen/drawable_padding"
    android:drawableStart="@drawable/ic_address"
    android:gravity="start|center_vertical"
    android:singleLine="true" />  
</RelativeLayout>  

And this layout lies within another one, using the <include> tag:

<CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<ScrollView
    android:id="@+id/scrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/appbar"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:background="@android:color/transparent" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@android:color/transparent" >

        <include
            android:id="@+id/btn_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            layout="@layout/btn_layout" />
    </RelativeLayout>
</ScrollView>
</CoordinatorLayout>

Solution
Well, that is awkward, but I just had a global Button style setting paddingLeft ... I was sure an xml declaration overrides style ... Apparently not always. Is it a bug or shouldn't I trust this behavior?
Anyway, issue finally solved:)

try this..if this matches your requirement :

  <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:gravity="center"
        android:padding="10dip"
        android:drawablePadding="10dip"
        android:drawableLeft="@drawable/ic_launcher"
        android:background="@drawable/preview_dailog_bg"
        android:text="swipe_element"
        android:textColor="@android:color/white"
        android:layout_centerInParent="true"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM