以编程方式更改浮动按钮背景 (ButterKnife)
Change the floatingButton Background (ButterKnife) programmatically
我是 Android Butterknife 的新手,想更改浮动按钮的背景颜色。我无法做到 this.How 我可以在 advance.Application 中实现 this.Thanks 当我使用时崩溃
floatingActionButton.setBackgroundTintList(ColorStateList.valueOf(Color
.parseColor("#33691E")));
如果我不集成 Butterknife,这可以正常工作。
这是我的浮动按钮
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:id="@+id/fab1"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
app:rippleColor="@android:color/white"
android:layout_margin="16dp"
android:src="@drawable/circle"
android:onClick="newForm"
app:layout_anchorGravity="bottom|right|end" />
并且在 MainActivity
public class MainActivity extends AppCompatActivity implements
SearchView.OnQueryTextListener {
@BindView(R.id.fab1)
FloatingActionButton floatingActionButton;
@BindColor(R.color.colorFolatingButton)
int Floating_Button_Color;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
setSupportActionBar(toolbar);
floatingActionButton.setBackgroundColor(Floating_Button_Color);
}
@OnClick(R.id.fab1)
public void newForm (View view){
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Confirm");
builder.setMessage("Are you sure?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(getApplicationContext(),BuilderPage.class);
startActivity(intent);
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
使用app:backgroundTint="@color/your_color"
我从@tdamian-kozlakejjd 找到了这个答案。按照这个 link 这可能对你有帮助
app:backgroundTint="@color/YOURCOLOR"
xmlns:app="http://schemas.android.com/apk/res-auto"
我是 Android Butterknife 的新手,想更改浮动按钮的背景颜色。我无法做到 this.How 我可以在 advance.Application 中实现 this.Thanks 当我使用时崩溃
floatingActionButton.setBackgroundTintList(ColorStateList.valueOf(Color
.parseColor("#33691E")));
如果我不集成 Butterknife,这可以正常工作。 这是我的浮动按钮
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:id="@+id/fab1"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
app:rippleColor="@android:color/white"
android:layout_margin="16dp"
android:src="@drawable/circle"
android:onClick="newForm"
app:layout_anchorGravity="bottom|right|end" />
并且在 MainActivity
public class MainActivity extends AppCompatActivity implements
SearchView.OnQueryTextListener {
@BindView(R.id.fab1)
FloatingActionButton floatingActionButton;
@BindColor(R.color.colorFolatingButton)
int Floating_Button_Color;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
setSupportActionBar(toolbar);
floatingActionButton.setBackgroundColor(Floating_Button_Color);
}
@OnClick(R.id.fab1)
public void newForm (View view){
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Confirm");
builder.setMessage("Are you sure?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(getApplicationContext(),BuilderPage.class);
startActivity(intent);
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
使用app:backgroundTint="@color/your_color"
我从@tdamian-kozlakejjd 找到了这个答案。按照这个 link 这可能对你有帮助
app:backgroundTint="@color/YOURCOLOR"
xmlns:app="http://schemas.android.com/apk/res-auto"