Android TableRow背景 [英] Android TableRow background

查看:117
本文介绍了Android TableRow背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在动态添加TableRow时遇到问题.

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);       
    }

我希望此行在onClickonLongClick时更改颜色.

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

customrow.xml文件中的代码是:

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

我已经尝试在行中使用android:background="@drawable/clickedbackground",但是它不起作用.

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

clickedbackground.xml文件中的代码为:

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

有人知道我在做什么错吗(颜色/自定义是在另一个有效的xml中定义的)?

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

谢谢

推荐答案

添加

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

在styles.xml和设置中

in styles.xml and setting

style="@style/row"

在TableRow中完成了工作.

in the TableRow did the job.

可绘制对象中的rows.xml所在的位置

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>

这篇关于Android TableRow背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆