2014-12-09 19 views
3

我有這樣是怎樣煉成的LinearLayout可點擊

<LinearLayout 
    android:id="@+id/optionlist" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/divider" 
    android:orientation="vertical" > 

</LinearLayout> 

我想

for (int i = 0; i < opt.length; i++) { 

     View view = inflater.inflate(R.layout.option_item, optionlist, 
       false); 
     view.setBackgroundResource(R.drawable.optionlist); 
     view.setTag(opt_image[i]); 
     view.setClickable(true); 
     view.setFocusable(true); 

     view.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // ADD your action here 

       int res = (Integer) v.getTag(); 

       switch (res) { 
       case R.drawable.logout: 
        signout(); 
        break; 
       } 

      } 
     }); 
     final TextView tv = (TextView) view.findViewById(R.id.option_name); 
     final ImageView iv = (ImageView) view 
       .findViewById(R.id.option_image); 
     tv.setText(opt[i]); 
     iv.setImageResource(opt_image[i]); 
     optionlist.addView(view); 
    } 

現在上添加另一個XML視圖到這個佈局

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/option" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <!-- 
       <View 
       android:layout_width="match_parent" 
       android:layout_height="0.5dip" 
       android:layout_marginLeft="10dp" 
       android:layout_marginRight="10dp" 
       android:background="#2fd275" 
       /> 
    --> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:padding="10dp" > 

     <ImageView 
      android:id="@+id/option_image" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:src="@drawable/logout" /> 

     <TextView 
      android:id="@+id/option_id" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="5dp" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:textColor="#000000" 
      android:visibility="gone" /> 

     <TextView 
      android:id="@+id/option_name" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:layout_margin="10dp" 
      android:text="Signout" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:textColor="#616161" /> 
    </LinearLayout> 

    <View 
     android:id="@+id/divider" 
     android:layout_width="match_parent" 
     android:layout_height="0.5dip" 
     android:background="#d3d3d3" /> 

</LinearLayout> 

通過代碼的LinearLayout點擊佈局我想加載這個xml文件,如下所示......但我無法獲得點擊事件加載的背景。請給我建議我做錯了什麼?

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:drawable="@color/holo_green_dark" android:state_focused="true"/> 
    <item android:drawable="@color/holo_green_dark" android:state_enabled="false" android:state_pressed="true"/> 
    <item android:drawable="@color/white"/> 

</selector> 

回答

2

試試這個..希望它的工作原理..

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

只需創建新的XML和繪製添加此代碼,並設置UR線性layoyut backgrout正如它的名字一樣android:background="@drawable/createdxmlname"

<LinearLayout 
     android:id="@+id/optionlist" 
     android:layout_below="@id/divider" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:background="@drawable/createdxmlname" 
     android:clickable="true"> 
+0

@M S Gadag我已經有佈局可見了,我想改變其點擊事件佈局的背景。 – 2014-12-09 11:59:47

+0

好..請問我是背景是圖像還是顏色? – 2014-12-09 12:01:18

+0

@JayDhamsaniya檢查我編輯好的答案... – 2014-12-09 12:07:56

0
android:clickable="false" 


<LinearLayout 
     android:id="@+id/parent" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 
     <ImageButton 
      android:clickable="false" 
      android:layout_width="40dp" 
      android:layout_height="40dp" 
      android:text="Cinema" 
      android:textSize="13sp" 
      android:layout_marginRight="10dp" /> 
     <Button 
      android:clickable="false" 
      android:layout_width="match_parent" 
      android:layout_height="40dp" 
      android:text="Check in" 
      android:textSize="13sp" /> 
</LinearLayout> 
3

首先從xml中獲取LinearLayout的id,然後對其執行onClickListener而不是整個視圖

View view = inflater.inflate(R.layout.option_item, optionlist, false); 
LinearLayout layout = view.findViewByIt(R.id.optionlist); 
layout.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     // ADD your action here 
     int res=(Integer) v.getTag(); 
     switch(res) 
     { 
     case R.drawable.logout: signout(); 
           break; 
     } 
    } 
}); 
+0

的背景=「?attr/selectableItemBackground」'像魅力一樣工作 – MoolsBytheway 2017-04-04 01:03:26

0

,內容如下

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" 
     android:drawable="@drawable/button_pressed" /> <!-- pressed --> 
    <item android:state_focused="true" 
     android:drawable="@drawable/button_focused" /> <!-- focused --> 
    <item android:drawable="@drawable/button_normal" /> <!-- default --> 
</selector> 

而且在佈局其添加爲背景資源創建的繪製pressed_layout.xml:在

<LinearLayout 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:background="@drawable/pressed_layout" 
    android:clickable="true"> 
</LinearLayout> 
0

layout.setclickble = 「真」 XML文件和

layout.setOnLongClickListener(new View.OnLongClickListener(){

 @Override 
     public boolean onLongClick(View arg0) { 
      // TODO Auto-generated method stub 

     } 
    }); 
-1

我們也可以在佈局的XML中使用onClick,並在java文件中使用該方法,如果onClick =「next」,那麼在java文件中我們必須使用該方法。

public void next() 
{ 
// do something 
} 
相關問題