在TextView的选定状态中包含父填充

时间:2013-04-09 01:16:53

标签: android relativelayout textview selecteditem

我有一个带有填充的RelativeLayout和一个TextView孩子。选择TextView后,我希望突出显示的区域扩展到包含父级的填充,以便突出显示整个RelativeLayout

main.xml

<TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Hello World, MyActivity"
        android:textSize="30dp"
        android:clickable="true"
        android:background="@drawable/myback"
        />

myback.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:drawable="@color/gray" />
    <item android:state_focused="true" android:drawable="@color/gray" />
    <item android:state_pressed="true" android:drawable="@color/gray" />
</selector>

MyActivity.java

package com.example.TextViewPadding;

import android.app.Activity;
import android.os.Bundle;

public class MyActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

enter image description here

1 个答案:

答案 0 :(得分:1)

如果您想要出现此行为,则需要将填充添加到TextView而不是RelativeLayout将父级的填充保持为0。