2012-10-03 39 views
0

我有一個TableLayout,我想要裏面的元素(每個錶行中有一個文本視圖)要集中ontouch。我將每個元素的可聚焦屬性設置爲true,但它不起作用!焦點錶行不工作

這是XML:

<?xml version="1.0" encoding="UTF-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:layout_gravity="bottom" 
android:focusable="true" 
android:focusableInTouchMode="true" 
android:gravity="right" > 


<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentTop="true" 
    android:background="#444444" 
    android:focusable="true" 
    android:focusableInTouchMode="true" 
    android:gravity="bottom" 
    android:orientation="vertical" > 

    <TableLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:focusable="true" 
     android:focusableInTouchMode="true" > 

     <requestFocus /> 


     <TableRow 
      android:id="@+id/tableRow1" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="#993399" 
      android:clickable="true" 
      android:focusable="true" 
      android:focusableInTouchMode="true" > 





      <TextView 
       android:id="@+id/textView1" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_marginRight="5dip" 
       android:layout_weight="1" 
       android:clickable="true" 
       android:editable="false" 
       android:focusable="true" 
       android:focusableInTouchMode="true" 
       android:gravity="right" 
       android:text="Zein" 
       android:textAppearance="?android:attr/textAppearanceMedium" /> 

     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow2" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:focusable="true" 
      android:focusableInTouchMode="true" > 

      <TextView 
       android:id="@+id/textView2" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:focusable="true" 
       android:focusableInTouchMode="true" 
       android:text="Home" 
       android:textAppearance="?android:attr/textAppearanceMedium" /> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow3" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:focusable="true" 
      android:focusableInTouchMode="true" > 

      <TextView 
       android:id="@+id/textView3" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:focusable="true" 
       android:focusableInTouchMode="true" 
       android:text="About us" 
       android:textAppearance="?android:attr/textAppearanceMedium" /> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow4" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="#00FF00" 
      android:focusable="true" 
      android:focusableInTouchMode="true" > 

      <TextView 
       android:id="@+id/textView4" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:focusable="true" 
       android:focusableInTouchMode="true" 
       android:gravity="right" 
       android:text="Students" 
       android:textAppearance="?android:attr/textAppearanceMedium" /> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow5" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:focusable="true" 
      android:focusableInTouchMode="true" > 

      <TextView 
       android:id="@+id/textView5" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_marginRight="5dip" 
       android:layout_weight="1" 
       android:focusable="true" 
       android:focusableInTouchMode="true" 
       android:gravity="left" 
       android:text="Course Scheduele" 
       android:textAppearance="?android:attr/textAppearanceMedium" /> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow6" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:focusable="true" 
      android:focusableInTouchMode="true" > 

      <TextView 
       android:id="@+id/textView6" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_marginRight="5dip" 
       android:layout_weight="1" 
       android:focusable="true" 
       android:focusableInTouchMode="true" 
       android:gravity="left" 
       android:text="Exam Scheduele" 
       android:textAppearance="?android:attr/textAppearanceMedium" /> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow7" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:focusable="true" 
      android:focusableInTouchMode="true" > 

      <TextView 
       android:id="@+id/textView7" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:focusable="true" 
       android:focusableInTouchMode="true" 
       android:text="Exam Grades" 
       android:textAppearance="?android:attr/textAppearanceMedium" /> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow8" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:focusable="true" 
      android:focusableInTouchMode="true" > 

      <TextView 
       android:id="@+id/textView8" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:focusable="true" 
       android:focusableInTouchMode="true" 
       android:text="Course Grades" 
       android:textAppearance="?android:attr/textAppearanceMedium" /> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow9" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:focusable="true" 
      android:focusableInTouchMode="true" > 

      <TextView 
       android:id="@+id/textView9" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:focusable="true" 
       android:focusableInTouchMode="true" 
       android:text="GPA" 
       android:textAppearance="?android:attr/textAppearanceMedium" /> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow10" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:focusable="true" 
      android:focusableInTouchMode="true" > 

      <TextView 
       android:id="@+id/textView10" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:focusable="true" 
       android:focusableInTouchMode="true" 
       android:text="Drop &amp; Add" 
       android:textAppearance="?android:attr/textAppearanceMedium" /> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow11" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:focusable="true" 
      android:focusableInTouchMode="true" > 

      <TextView 
       android:id="@+id/textView11" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:focusable="true" 
       android:focusableInTouchMode="true" 
       android:text="Petition Enquiry" 
       android:textAppearance="?android:attr/textAppearanceMedium" /> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow12" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:focusable="true" 
      android:focusableInTouchMode="true" > 

      <TextView 
       android:id="@+id/textView12" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:background="#00FFFF" 
       android:focusable="true" 
       android:focusableInTouchMode="true" 
       android:gravity="right" 
       android:text="University Services" 
       android:textAppearance="?android:attr/textAppearanceMedium" /> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow13" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:focusable="true" 
      android:focusableInTouchMode="true" > 

      <TextView 
       android:id="@+id/textView13" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:focusable="true" 
       android:focusableInTouchMode="true" 
       android:text="Petition Enquiry" 
       android:textAppearance="?android:attr/textAppearanceMedium" /> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow14" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="#D60024" 
      android:focusable="true" 
      android:focusableInTouchMode="true" > 

      <TextView 
       android:id="@+id/textView14" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:focusable="true" 
       android:focusableInTouchMode="true" 
       android:gravity="right" 
       android:text="Employees" 
       android:textAppearance="?android:attr/textAppearanceMedium" /> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow15" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:focusable="true" 
      android:focusableInTouchMode="true" > 

      <TextView 
       android:id="@+id/textView15" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:focusable="true" 
       android:focusableInTouchMode="true" 
       android:text="Petition Enquiry" 
       android:textAppearance="?android:attr/textAppearanceMedium" /> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow16" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="#009933" 
      android:focusable="true" 
      android:focusableInTouchMode="true" > 

      <TextView 
       android:id="@+id/textView16" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_marginRight="5dip" 
       android:layout_weight="1" 
       android:focusable="true" 
       android:focusableInTouchMode="true" 
       android:gravity="right" 
       android:text="My Account" 
       android:textAppearance="?android:attr/textAppearanceMedium" /> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow17" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:focusable="true" 
      android:focusableInTouchMode="true" > 

      <TextView 
       android:id="@+id/textView17" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:focusable="true" 
       android:focusableInTouchMode="true" 
       android:text="Petition Enquiry" 
       android:textAppearance="?android:attr/textAppearanceMedium" /> 
     </TableRow> 
    </TableLayout> 
</LinearLayout> 

我想每一個元素是重點,當用戶觸摸它, 任何想法,爲什麼它不工作在這裏嗎?

繪製/ table.xml

UPDATE:

<?xml version="1.0" encoding="utf-8"?> 
    <selector xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item android:state_pressed="true" 
      android:color="#993399" /> <!-- pressed --> 
    <item android:state_focused="true" 
      android:color="#993399" /> <!-- focused --> 
    <item android:color="#222222" /> <!-- default --> 

</selector> 

這是怎麼我指的是我的main.xml內

android:src="@drawable/table1" 

仍然什麼也沒有發生

回答

0

爲你的listview創建選擇器TEM,並設置其參數,例如:

<item android:drawable="@drawable/but" android:state_pressed="false" android:state_selected="false"/> 
<item android:drawable="@drawable/butpressed" android:state_pressed="true"/> 
<item android:drawable="@drawable/butpressed" android:state_pressed="false" android:state_selected="true"/> 

在我改變圖片的例子,但你可以設置任何其他參數

+0

可以請您詳細闡述更多的我真的不太明白你所說的 –

+0

http://stackoverflow.com/questions/1219312/android-selector-text-color 這裏是選擇器的例子。這個想法是編程按鈕的不同狀態。您可以對TableLayout字段使用相同的想法 – SVS

+0

org.xmlpull.v1.XmlPullParserException:二進制XML文件行#4:標記需要「drawable」屬性或子標記定義可繪製 \t at android.graphics.drawable.StateListDrawable .inflate(StateListDrawable.java:178) –