【问题标题】:Change the floatingButton Background (ButterKnife) programmatically以编程方式更改浮动按钮背景(ButterKnife)
【发布时间】:2016-08-23 04:57:52
【问题描述】:

我是 Android Butterknife 的新手,想更改浮动按钮的背景颜色。我无法做到这一点。我怎样才能做到这一点。提前致谢。我使用时应用程序崩溃。

 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();
}

【问题讨论】:

  • 在使用 Butterknife 之前 setBackgroundTintList 工作正常,当我使用 Butterknife 应用程序时突然崩溃...

标签: android-studio butterknife


【解决方案1】:

使用app:backgroundTint="@color/your_color"

【讨论】:

  • 我正在尝试以编程方式更改它。但这也很好用。谢谢。
【解决方案2】:

我从@tdamian-kozlakejjd 找到了这个答案。按照这个链接这可能会帮助你 How to change FAB background color

app:backgroundTint="@color/YOURCOLOR"
xmlns:app="http://schemas.android.com/apk/res-auto" 

【讨论】:

  • 应将较新的问题标记为重复关闭,而不是在问题之间复制答案。
  • 谢谢,下次不要这样了。我是新手。
猜你喜欢
  • 2011-03-07
  • 2020-05-15
  • 2013-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-28
相关资源
最近更新 更多