2012-03-30 12 views
0

私は、RelativeLayoutを使用してボタンを作成したいので、内部に多くのドロウアブルやテキストフィールドを入れることができます。また、すべての子どもが色や描画可能な部分をRelativeLayout状態に変更します。 カスタムボタンを押すと、正しい色と描画可能なものが必要です。ここでカスタムボタン処理状態

は、いくつかのコード(一つだけのTextView例えばImageViewの)です:では

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

:main.xmlで

、親のレイアウトはのLinearLayout、ここにカスタムボタンがあります描画可能なフォルダ、imageview_state.xml:描画可能-hdpiフォルダで

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

鼻くそによって示唆されるように、私は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を使用して変更が、そう遅れが見えた。もうセレクタに言及していない

android:background="@color/background" 

: は今RelativeLayoutの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

+0

次に、onClickの代わりにsetOnTouchListenerを使用してレイアウトに触れるときに処理します。 – Booger

関連する問題