1

2 つの個別のカスタム XML ボタン定義を定義する際に奇妙な問題が発生しています。エラーを再現するためだけにテスト プロジェクトを作成したので、完全なコードと問題のスクリーンショットを追加します。基本的には、再利用可能なカスタム XML ボタン構成を定義するために 9 パッチ イメージを使用したいと考えています。

2 つの XML ファイルを定義しました。

z_btn_xml_glossy_blue_lightblue.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:state_focused="true"
        android:drawable="@drawable/z_btn_glossy_lightblue" >
        <corners
            android:radius="3dp" />
        <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />
    </item>

    <item
        android:state_pressed="true"
        android:drawable="@drawable/z_btn_glossy_lightblue" >
        <corners
            android:radius="3dp" />
        <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />        
    </item>

    <item
        android:drawable="@drawable/z_btn_glossy_blue" >
        <corners
            android:radius="3dp" />
        <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />        
    </item>
</selector>

z_btn_xml_glossy_black_white.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:state_focused="true"
        android:drawable="@drawable/z_btn_glossy_white" >
        <corners
            android:radius="3dp" />
        <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />
    </item>

    <item
        android:state_pressed="true"
        android:drawable="@drawable/z_btn_glossy_white" >
        <corners
            android:radius="3dp" />
        <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />        
    </item>

    <item
        android:drawable="@drawable/z_btn_glossy_black" >
        <corners
            android:radius="3dp" />
        <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />        
    </item>
</selector>

ご覧のとおり、これらのファイルはどちらも基本的にまったく同じですが、異なるドローアブルを使用しています。私の main.xml レイアウト ファイルは次のとおりです。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="5dip"
    android:paddingRight="5dip"
    android:paddingTop="0dip"
    android:paddingBottom="3dip" >
    <Button
        android:id="@+id/BTNHostWaitingStartGame"
        android:background="@drawable/z_btn_xml_glossy_blue_lightblue"
        android:textColor="#FFFFFF"
        android:textSize="18sp"
        android:layout_marginRight="2dip"
        android:layout_width="wrap_content"
        android:layout_height="45dip"
        android:layout_weight="1.0"
        android:text="Start Game" />
    <Button
        android:id="@+id/BTNHostWaitingCancelGame"
        android:background="@drawable/z_btn_xml_glossy_black_white"
        android:textColor="#FFFFFF"
        android:textSize="18sp"
        android:layout_marginLeft="2dip"
        android:layout_width="wrap_content"
        android:layout_height="45dip"
        android:layout_weight="1.0"
        android:text="Cancel Game" />
</LinearLayout>

かなり単純化して、2 つのボタンを作成するだけです。レイアウトを R.layout.main に設定するだけなので、テスト アクティビティ ファイルは投稿しません。

これは、Eclipse 内のグラフィカル レイアウトのスクリーンショットです。これは、ボタンをどのように表示したいかのように見えます。

グラフィカルなレイアウト、すべてが良さそう

ただし、エミュレーターでは、次のようになります。

2 番目のボタンのスタイルが設定されていません

ご覧のとおり、2 番目のボタンはスタイル設定されていません。これがなぜなのか、誰かが光を当てることができますか? デバイスでも同様です。私が気付いていない、または見ていないだけの制限はありますか?

助けてくれてありがとう!

4

1 に答える 1

2

それは私のマシンで動作します。

私はあなたのコードを取り、セレクターxmlをres/drawableに配置しました。s内の描画可能な参照itemが画像(たとえば、z_btn_glossy_lightblueres / drawable-hdpi / z_btn_glossy_lightblue.pngを参照)であると仮定して、これらの参照を自分のいくつかの画像に交換しました。チャームのように機能します。

試す:

  • xmlが正しいフォルダーに配置されていることを確認し、これらのファイルのコピーが誤って別のフォルダーに配置されていないことを確認してください。
  • アイテムで参照するドローアブルに、res / drawable /z_btn_glossy_lightblueなどの同名の名前がないことを確認してください。xml
  • 他のボタンで機能することがわかっている他の画像を参照してテストします。
于 2011-05-25T10:04:03.870 に答える