2012-03-30 124 views
0

我只是想用一個RelativeLayout來製作一個按鈕,所以我可以在裏面放置儘可能多的drawables或textfields,並且我希望所有的孩子都可以改變顏色或者可以繪製到RelativeLayout狀態。 如果我按下自定義按鈕,它必須具有正確的顏色和可繪製的。自定義按鈕處理狀態

下面是一些代碼(僅一個的TextView和用於例如ImageView的):

main.xml中,父佈局是一個的LinearLayout,這裏是自定義按鈕:

<RelativeLayout 
    android:id="@+id/relativelayout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="10dip" 
    android:layout_marginRight="10dip" 
    android:layout_marginTop="25dp" 
    android:background="@drawable/background_state" 
    android:clickable="true" 
    android:focusable="true" > 

    <TextView 
     android:id="@+id/textview" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:layout_marginLeft="8dip" 
     android:clickable="true" 
     android:text="Awesome text" 
     android:textColor="@color/textview_state" 
     android:textSize="20sp" /> 

    <ImageView 
     android:id="@+id/imageview" 
     android:layout_width="51dip" 
     android:layout_height="51dip" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:layout_marginRight="5dip" 
     android:clickable="true" 
     android:src="@drawable/imageview_state" /> 

    <View 
     android:layout_width="wrap_content" 
     android:layout_height="1dp" 
     android:layout_alignParentTop="true" 
     android:background="#FFFFFF" 
     android:clickable="false" 
     android:focusable="false" /> 

    <View 
     android:layout_width="wrap_content" 
     android:layout_height="1dp" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="50dip" 
     android:background="#D3E992" 
     android:clickable="false" 
     android:focusable="false" /> 

</RelativeLayout> 

在提拉文件夾,imageview_state.xml:

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

在提拉 - 華電國際文件夾,裏面是imageview.png和imageview_pressed.png。

在顏色的文件夾,background_state.xml:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" android:drawable="#E0EBCC"/> 
    <item android:drawable="#669900"/> 

</selector> 

在顏色的文件夾,textview_state.xml:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" 
      android:color="#000"/> 
    <item android:color="#FFF"/> 
</selector> 

到目前爲止,當我點擊的RelativeLayout中,RelativeLayout的背景變化正確,但ImageView和TextView保持默認狀態。 我試着爲TextView和ImageView設置屬性android:clickable =「true」,但只有按下的元素會改變它的狀態,而不是整個按鈕。

任何幫助來實現我的目標是歡迎的。

回答

0

正如Booger所建議的那樣,我使用了一個OnTouchListener,並且我基本上在內部進行了更改。它完美的作品,我會首選一個XML解決方案,但無論如何。 在我的活動:

RelativeLayout relativeLayout= (RelativeLayout) findViewById(R.id.relativelayout); 
relativeLayout.setOnTouchListener(...); 

聽者的實施:

public boolean onTouch(View v, MotionEvent event) 
    { 
     TextView text = (TextView) findViewById(R.id.textview); 
     ImageView image = (ImageView) findViewById(R.id.imageview); 
     RelativeLayout layout = (RelativeLayout) findViewById(R.id.relativelayout); 
     if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) 
     { 
      layout.setBackgroundResource(R.color.background_pressed); 
      text.setPressed(true); 
      image.setPressed(true); 

     } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) 
     { 
      layout.setBackgroundResource(R._background); 
      text.setPressed(false); 
      image.setPressed(false); 
     } 
     return false; 
    } 

我做的一切程序,雖然RelativeLayout的背景改變其狀態正確使用XML,因爲XML比代碼快,所以延遲是可見的。 現在RelativeLayout的背景屬性設置爲:

android:background="@color/background" 

不是指一個選擇了。

0

您可以在相對佈局設置一個OnClickListener,那麼就做你的代碼的東西被點擊時它。像這樣的僞代碼。

RelativeLayout rl = (RelativeLayout) findViewById(R.id.myRelLayout); 
rl.setOnClickListener(.....) { 
    button1.setBackgroudDrawable(R.drawable.newColor). 
    text1.setText("Something"); 
} 
+0

我試過了,但這不是我要找的,例如: 如果用戶按住按鈕,Click事件將不會被觸發,背景將正確地具有按下狀態的背景,但TextView和ImageView不會。 – XST 2012-03-30 18:07:25

+0

然後只需setOnTouchListener而不是onClick來處理他們觸摸您的佈局時。 – Booger 2012-03-30 21:51:36