1

画像が必要で、画像の上に短い半透明のビューを配置し、そのビュー内にテキストビューを配置し、最後にボタンのような基本的なクリック効果を示すこれらすべてをクリック可能にしたい.

使えない

<Button>
   <ImageView>
      <TextView/>
   </ImageView>
</Button>

Clickable=true で LinearLayout を作成できます。しかし、ボタンに存在する視覚的なプレス効果をどのように模倣できますか?

更新:どのように表示されるかを示す画像を追加しました。これは ImageButton のようなものだと考えてください (オンプレス効果が必要です)。

ここに画像の説明を入力

更新-2:押された効果を示す別の画像を追加しました。(Play ストア アプリに似ています)。 ここに画像の説明を入力

4

3 に答える 3

1

選択したレイアウトを使用して、必要な効果を作成し、レイアウトにスタイルを設定します@android:style/Widget.Button

setWillNotDraw(false)また、レイアウトを呼び出す必要がある場合もあります。

于 2013-02-26T15:28:31.200 に答える
1

しかし、ボタンに存在する視覚的なプレス効果をどのように模倣できますか?

私が間違っていなければ、LinearLayout に android:background="@drawable/some_click" を追加できます

some_click.xml
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true" 
android:drawable="@drawable/pressed" /> <!-- pressed -->
<item
android:state_focused="true" 
android:drawable="@drawable/focused" /> <!-- focused -->
<item
android:drawable="@drawable/default" /> <!-- default -->
</selector>
于 2013-02-26T15:29:45.017 に答える
1

画像やテキストなどを含む ViewGroup (LinearLayout/RelativeLayout/etc など) を作成します。

この ViewGroup をクリック可能にします (たとえば、OnClicListener を割り当てることによって)。

この ViewGroup に 背景のドローアブルを割り当てます。

このバックグラウンド ドローアブルが状態リスト ドローアブルであることを確認してください: https://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html http://developer.android.com/guide/topics/resources/ drawable-resource.html#StateList

適切なドローアブルをさまざまな状態に割り当てます (最も重要なのは state_pressed ですが、他の状態も処理したい場合があります)。

ユーザーがボタンを押すと、適切なドローアブルが表示され、ViewGroup がボタン (押すことができるもの) であるかのように表示されます。

押された状態の新しい画像を表示する OP の後に更新します。

StateListDrawable を持つ背景を使用して、画像/テキスト/などの上にあるビューを追加します。

<RelativeLayout >
    <ImageView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/somerealpngfile"
        ... 
    />
    <TextView  
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        ... 
    />
    <View 
        android:id+"@+id/clickable_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/selectable_background"
        android:clickable="true"

        ... 
    />
</RelativeLayout>

res/drawable/selectable_background.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/grid_state_pressed"/>
    <item android:state_focused="true" android:drawable="@color/grid_state_focused"/>
    <item android:drawable="@android:color/transparent"/>
</selector>

res/values/colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
  ...
  <color name="grid_state_pressed">#BB7dbcd3</color>
  <color name="grid_state_focused">#777dbcd3</color>
  ...
</resources>

ここで、色grid_state_pressedと色grid_state_focusedは半透明です (つまり、アルファは 255 未満です)。

ユーザーが「ボタン」をクリックすると、View withR.id.clickable_viewは onClick を処理し、その背景色を変更して、画像とテキストを半透明の方法で透過させます。

于 2013-02-26T15:32:58.413 に答える