Android TableRow background

2019-08-10 01:15发布

I have a problem with a TableRow, which I add dynamically.

private void addRow(String body) {
        LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        TableRow row = (TableRow) inflater.inflate(R.layout.customrow,null);
        TextView name = (TextView) row.findViewById(R.id.customName);
        name.setText(body);                        
        row.setOnLongClickListener(this);       
    }

I would like this row to change color upon onClick and onLongClick.

Code in the customrow.xml file is:

<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/tableRow1" 
       android:layout_gravity="center" 
       android:layout_height="wrap_content" 
       android:layout_width="fill_parent" 
       android:gravity="center_vertical" 
       android:onClick="showOnClick">    
    <TextView android:id="@+id/customName"
        android:textSize="25px"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:layout_weight="5">
    </TextView>
</TableRow>

I have tried to use android:background="@drawable/clickedbackground"with the row but it is not working.

Code in the clickedbackground.xml file is:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">        
    <item android:state_selected="true" android:state_focused="false"
        android:state_pressed="false" android:drawable="@android:color/transparent" />
    <item android:state_selected="false" android:state_focused="false"
        android:state_pressed="false" android:drawable="@android:color/transparent" />
    <item android:state_pressed="true" android:drawable="@color/custom" />
    <item android:state_focused="true" android:state_selected="true"
        android:state_pressed="false" android:drawable="@color/custom" />
</selector>

Anyone knows what I am doing wrong (color/custom is defined in another xml which works)?

Thank you

4条回答
姐就是有狂的资本
2楼-- · 2019-08-10 01:20

You are creating object for tablerow named row. and you have also clickedbackground.xml file. just use below code in addRow method.

row.setBackgroundResource(R.drawable.clickedbackground);

I think it solves your problem.

查看更多
Juvenile、少年°
3楼-- · 2019-08-10 01:27

do not forget to add to the style

<item name="android:focusable">true</item>
        <item name="android:clickable">true</item>

Otherwise, you will not be able to use the states of a rowLayout.

查看更多
走好不送
4楼-- · 2019-08-10 01:29

In your addRow() method you're inflating the row but you're not adding it to any parent layout, and as row is a local variable I think you're not doing it anywhere else, is it a copy/paste problem?

Again, your customrow.xml might be not working because the opening TableRow tag lacks the closing >, but it might be copy/paste problem.

Using android:background="@drawable/bg" with bg being a selector is a common pattern and it should work. You might want to simplify your selector: you don't need to specify all the states for each item and all the combinations. It works with a "first match", so this will do the job:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">        
    <item android:state_pressed="true" android:drawable="@color/custom" />
    <item android:state_selected="true" android:drawable="@android:color/transparent" />
    <item android:state_focused="true" android:drawable="@color/custom" />
    <item android:drawable="@android:color/transparent" />
</selector>

Also, notice that selected and focused are two different states, focused being the one you get when moving around with dpad.

If this didn't help please specify what "is not working" means: what do you expect? what's happening instead?

查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-08-10 01:33

Adding

<resources>
 <style name="row" parent="@android:style/Widget.Button">
     <item name="android:background">@drawable/rows</item>
</style>
</resources>

in styles.xml and setting

style="@style/row"

in the TableRow did the job.

where the rows.xml in the drawable is

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
    android:state_enabled="false"
        android:drawable="@color/blue" />
    <item
        android:state_pressed="true"
        android:state_enabled="true"
        android:drawable="@color/custom" />
    <item
        android:state_focused="true"
        android:state_enabled="true"
        android:drawable="@color/white" />
    <item
        android:state_enabled="true"
        android:drawable="@android:color/transparent" />
</selector>
查看更多
登录 后发表回答