アンドロイドのRelativeLayoutを継承した独自のボタンを作成しました。 私の質問は、自分のボタンにアンドロイドのボタンスタイル(バックグラウンドドロウアブル、パディング)を適用するにはどうすればいいですか?自分のボタンにアンドロイドのImageButtonスタイルを適用するにはどうすればよいですか?
ありがとうございます。
アンドロイドのRelativeLayoutを継承した独自のボタンを作成しました。 私の質問は、自分のボタンにアンドロイドのボタンスタイル(バックグラウンドドロウアブル、パディング)を適用するにはどうすればいいですか?自分のボタンにアンドロイドのImageButtonスタイルを適用するにはどうすればよいですか?
ありがとうございます。
in your drawable folder - create this xml say custom_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<solid
android:color="#E77A26" />
<stroke
android:width="1dp"
android:color="#E77A26" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#70c656"
android:endColor="#53933f"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#53933f" />
<corners
android:radius="4dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
And in your main layout -
<Button
android:id="@+id/connect"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="@drawable/custom_button"
android:layout_marginRight="10dp"
android:text="@string/connect" />
if you want to put styles to your text in that button - add a style resource in your strings.xml - like below
<style name="buttonText">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#ffffff</item>
<item name="android:gravity">center</item>
<item name="android:textSize">15dp</item>
<item name="android:textStyle">bold</item>
<item name="android:shadowColor">#000000</item>
<item name="android:shadowDx">1</item>
<item name="android:shadowDy">1</item>
<item name="android:shadowRadius">2</item>
</style>
You can use this style in your button as below,
style="@style/buttonText"
Thats it...
あなたが何を求めているかわからない。しかし、単にデフォルトのボタンの背景を探しているのであれば、
android:background="@android:drawable/btn_default_normal"
手作業で行う必要があるかもしれません。
このlinkは、アンドロイドのリソースのリストを提供します。
説明のために、カスタムボタンを作成するための相対レイアウトを継承する理由は何ですか?ボタンの外観を変更するだけでよい場合は、セレクタドロウアブルを指定してボタンの外観を変更することができます。
RelativeLayoutを継承する必要がある場合、ボタンの状態を処理する必要があるコードがもう少しあります。これがうまくいけば、blessenmが言及したように、背景セレクタ描画可能なものを添付することができます。
これは機能しません。リソースは公開されていません。これは私が得るものです:: error:Error:リソースは公開されていません。 (値 '@android:drawable/btn_default_normal'を持つ 'バックグラウンド')。 – michael