私のアプリケーション用のボタンをカスタマイズしたいと思います。アプリケーションには、ユーザーが色を選択するカラーピッカーがあり、その特定の開始/終了色をボタンに設定する必要があります。これらのコロ値は、オブジェクト「ユーティリティ」に格納されます。アンドロイド:動的値を持つカスタマイズボタン
基本的には、「ユーティリティ」オブジェクトを使用して、背景、テキストの色、フォントなどの色を設定したいと思います。また、ユーザーが色を変更したときに、それら。また、ファイルに色を保存するため、ユーザーが次にアプリケーションを起動するときに、最後に選択した色が表示されます。
xmlの色を変更することができないため、<selector>
が最適なオプションではありませんでした。そのような要件に最適な選択肢は何でしょうか?
更新: @jitendra、回答から私はsomethignが役立ちました。私はGradientDrawableを使ってボタンの色を設定します。私のonCreate()アクティビティでは、ボタンのルート、テキストの色/サイズ、およびグラデーションの色の背景を設定するRefreshComponents()メソッドを呼び出します。正常に動作しますが、唯一の問題は、GradientDrawableをボタンに適用することで、2つのボタンの間のギャップが失われることです。
これはGradientDrawableを適用せずに画像です:出力GradientDrawableを適用するには
は次のとおりです。
あなたは、ボタンのサイズはすべての側面から少し増加している参照してください。私が次のボタンにも当てはまると、彼らはお互いに触れる。上記のための私のXMLは次のとおりです。私が作成
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainroot" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical"
android:paddingTop="35dip" android:paddingBottom="35dip"
android:paddingLeft="35dip" android:paddingRight="35dip"android:gravity="center" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainrow1" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_marginBottom="15dip" android:gravity="center_horizontal" >
<Button android:text="Accounting" android:id="@+id/accBtn" android:layout_width="80dip" style="@style/TileButtonStyle" />
<Button android:text="Data" android:id="@+id/dataBtn" android:layout_width="80dip" android:layout_height="fill_parent"></Button>
<Button android:text="Information" android:id="@+id/infoBtn" android:layout_width="80dip" android:layout_height="fill_parent" android:ellipsize="end"></Button>
</LinearLayout>
..... Other lineasr layout with same parameters as above child
そしてGradientDrawableは次のとおりです。私のonCreate(中
public static GradientDrawable getButtonDrawable(Button btn) {
int colors[] = {getStartColor(), getEndColor()};
GradientDrawable grad = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, colors);
grad.setCornerRadius(5f);
return grad;
}
そして最後に)、私が追加:
GradientDrawable btnGradient = Utility.getButtonDrawable(btn1);
btn1.setBackgroundDrawable(btnGradient);
起こっていますここで間違っている?ボタン周りの余白は0になっていますか? gradの境界を設定する必要がありますか、ボタンのLayoutParamsを再度設定する必要がありますか?
私の目標を達成するための助けに感謝します。
おかげ
あなたは背景を変更しているとき、あなたのコード内でそれらのいずれかを設定している場合を除きレイアウトパラメータを更新する必要はありませんが、あなたが背景の変化にはいくつかのレイアウトパラメータを追加しているようです。 – jeet
@jitendrasharma、私の次の目標は、ボタンのサイズを自動的に変更するレイアウトを動的に変更することです。見て、助けてください - http://stackoverflow.com/questions/8769491/android-retrieve-layout-marginbottom-programmatically – Tvd
@jitendrasharma、私は各ボタンのLAyoutParamsを変更する予定はありません。上記はちょっとしたことだった。しかし実際にはmarginRightをボタンに追加するだけで仕事ができました。彼らが何か他のものになるはずですか、どこかで間違っているかもしれませんが、それを見つけることはできません。 – Tvd