2012-01-11 2 views
12

버튼 모양의 구성 요소를 만들려고합니다. 왼쪽 정렬 된 ImageView와 ImageView의 오른쪽에 2 개의 TextView가 있고, 다른 하나 위에 겹쳐서 형식이 다르게 구성되어 있습니다 다음 예제 :.선택 가능한 기능을 가진 버튼으로 Android 레이아웃

__________________________ 
|       | 
| |-----| Bold Main Text | 
| |Image|     | 
| |-----| Small Sub Text | 
|__________________________| 

나는 또한 이미지 뷰가 많은 그와 관련된 선택 가능한 자원으로 할 것 표준 버튼처럼, 외부 용기의 클릭 상태에 따라 변경하고 싶습니다. 그래서 외부 상자의 아무 곳이나 클릭하면 이미지 선택 가능 상태가 변경됩니다.

'drawableLeft'속성을 설정하여 버튼과 같이 이미지와 연결된 텍스트 한 줄을 만들 수 있지만이 전략을 사용하여 텍스트 한 항목 만 가질 수있는 것으로 알고 있습니다.

과거에 이런 UI 구성 요소를 구현 한 사람이 있습니까?

감사합니다.

+1

그럼 당신은 당신의 자신의 레이아웃을 사용하고,에 OnClickListener를 추가, 그래서 버튼처럼 begave 할 수 있습니다. 클릭 상태에서 이미지가 변경되면 청취자에게 좀 더 복잡한 로직이 필요할 것이고, 대신 onTouchListener를 사용하는 것이 좋습니다 – Guillaume

답변

14

ImageView 위젯에 android:duplicateParentState="true"을 추가 할 수 있습니다. 또한 ImageView의 부모를 클릭 할 수 있고 포커스를 설정할 수 있도록해야합니다.

다음 코드의 RelativeLayout

Button 역할을합니다 :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout:height="match_parent" 
    android:orientation="vertical"> 

    <RelativeLayout 
     android:id="@+id/button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:clickable="true" 
     android:focusable="true"> 

     <ImageView 
      android:id="@+id/image" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" 
      android:duplicateParentState="true" 
      android:src="@drawable/icon" /> 
     <TextView 
      android:id="@+id/text1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@+id/image" 
      android:layout_alignTop="@+id/image" 
      android:layout_alignWithParentIfMissing="true" 
      android:duplicateParentState="true" /> 
     <TextView 
      android:id="@+id/text2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"    
      android:layout_toRightOf="@+id/image" 
      android:layout_below="@+id/text1" 
      android:layout_alignWithParentIfMissing="true" 
      android:duplicateParentState="true" /> 
    </RelativeLayout> 
</LinearLayout> 
+1

Pixie 덕분에 완벽하게 작동했습니다. –

+0

당신을 환영합니다! 기쁜 데 도움이되었습니다. – Michael

+1

굉장한 해결책! 지금 몇 시간 동안이 일을하려고 노력 중입니다. 이것은 나의 하루를 보냈다. 감사! – VM4

관련 문제