4

全てにおいて良い日。

各行に 3 つの がありますTableLayout。行全体TextViewに追加することはまだ可能ですか? OnClickListener選択した行の背景色を変更したいです。次のようにしてを に設定しましOnClickListenerTableRowが、背景色は変わりません。

   for(int i =0; i < rowAmount; i++)
        {           
            TableRow tr= new TableRow(this);

            TextView rmNo;
            TextView s;
            TextView p;

            rmNo = new TextView(this);
            s = new TextView(this);
            p = new TextView(this);

            rmNo.setText("" + roomNumbers.get(i).toString());
            s.setText("" + statuses.get(i).toString());
            p.setText("" + priorities.get(i).toString());

            tr.addView(rmNo);
            tr.addView(s);
            tr.addView(p);      

            tr.setOnClickListener(new View.OnClickListener() 
            {                   
                @Override
                public void onClick(View v)
                {
                    tr.setBackgroundColor(color.holo_blue_light);
                }
            });

            tblContent.addView(tr);
        }

    }

データはデータベースから取得されるため、TableRows と TextViews をプログラムで作成しています。

これは XML です。

<ScrollView
    android:id="@+id/scroll"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tblTitles">   
    <TableLayout 
        android:id="@+id/tblContent"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/ob">


   </TableLayout>
</ScrollView> 

どんな助け/アイデアも大歓迎です。

すでに検索されたソース:

クリック時にテーブルの行を強調表示するにはどうすればよいですか?

フォーカス時にTableRowの背景色を変更するには?

4

4 に答える 4

1

shape.xml

<selector
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" >
        <shape>
            <gradient
                android:startColor="#DDA0DD"
                android:endColor="#ffff00"
                android:angle="270" />
                       <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>

    </item>

    <item android:state_focused="true" >
        <shape>
            <gradient
                android:endColor="#ff00ff"
                android:startColor="#000fff"
                android:angle="270" />
            <stroke
                android:width="3dp"
                android:color="#f0f0f0" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>

    <item>        
        <shape>
            <gradient
                android:endColor="#000000"
                android:startColor="#000000"
                android:angle="270" />
            <stroke
                android:width="1dp"

                android:color="#000000" />
            <corners
                android:radius="3dp" 
                />
            <padding
                android:left="1dp"
                android:top="1dp"
                android:right="1dp"
                android:bottom="10dp" />
        </shape>

    </item>

</selector>

それから加えて

 tr.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape));
于 2013-07-30T09:55:53.470 に答える
1

あなたは使うことができます tr.setOnClickListener(this);

それ以外の

tr.setOnClickListener(new View.OnClickListener() 
            {                   
                @Override
                public void onClick(View v)
                {
                    tr.setBackgroundColor(color.holo_blue_light);
                }
            });

それから

 @Override
                public void onClick(View v)
                {
//you can set background color of 'v'
v.setBackgroundColor(color.holo_blue_light);
                }
于 2013-07-30T09:56:14.897 に答える