2012-10-24 3 views
1

이미지 및 텍스트를 포함 할 수있는 버튼에 대한 맞춤 컨트롤을 개발했습니다.이를 수행 할 수있는 표준 위젯을 찾지 못했기 때문입니다. . 그것은 좋은 일하지만, 지금 내가 무엇을 실행, 그 버튼을 클릭하면, textview 그것을 누르면 상태를 변경하지 않습니다. 이 동작을 적용하여 텍스트 뷰에 ColorStateList을 적용하여 단추를 눌렀을 때 텍스트 색이 변경되도록하고 싶습니다. 이 사용자 정의 버튼의 내 XML 레이아웃은 다음과 같습니다Android : 동일한 레이아웃에서 버튼의 상태와 텍스트 뷰의 상태를 연결하는 방법

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/image_text_button" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_gravity="center" 
    android:background="@drawable/btn_background" 
    android:visibility="visible" 

    > 

    <LinearLayout android:id="@+id/layoutContent" 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:gravity="center" 
     android:orientation="vertical" 
     android:padding="3dp" 
     android:duplicateParentState="true"> 

     <ImageView 
      android:id="@+id/imageView" 
      android:layout_width="wrap_content" 
      android:layout_height="0dp" 
      android:layout_weight="2" 
      android:duplicateParentState="true" 
      android:gravity="bottom|center_horizontal" 
      android:src="@drawable/testing_teaser1" 
      /> 

     <TextView 
      android:id="@+id/textField" 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:duplicateParentState="true" 
      android:gravity="center" 
      android:text="This is a ImageTextButton" 
      android:textColor="@color/black" /> 


    </LinearLayout> 

    <Button 
     android:id="@+id/button" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="@drawable/btn_bg_selector" 
     /> 

</FrameLayout> 

가 어떻게 버튼 상태로 텍스트 뷰를 링크 할 수 있습니다 ? 버튼이 텍스트 뷰의 부모가 아니기 때문에 android:duplicateParentState="true"이 작동하지 않습니다.

건배 야

답변

4

포함 Button xml에 추가해야합니다. android:duplicateParentState="true"

+0

아주 좋은 솔루션입니다. 매력처럼 작동합니다! 그러나 Button을 'android : clickable = false'로 설정하여 클릭을 가로 채지 않도록해야했습니다. – Dude

1

당신은 정확히를 않는 위젯이 있음을 알아? 당신은 여전히 ​​사용자 정의 구성 요소와 함께 가고 싶은 경우 (ImageButton)

, 당신은 당신의 부모 FrameLayout에 "@ 드로어 블/btn_bg_selector"를 할당하고 Button 대신 그 레이아웃 clickable을해야한다. 버튼 XML에서

+0

텍스트를 이미지 아래에 나타나는 ImageButton? – Dude

-1

는 속성

android:onClick="clickAction" 

을 추가하고 활동 단지 OnClickListenerLinearLayout 또는 대신 Button로 설정 FrameLayout 설정된 다음 함수를

public void clickAction(View view){ 
    //Code whatever you want when the button is clicked 
} 
관련 문제