首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何为按钮分配不同的背景色?

如何为按钮分配不同的背景色?
EN

Stack Overflow用户
提问于 2015-09-02 14:04:46
回答 3查看 80关注 0票数 0

我需要为button.For示例设置不同的颜色,当我单击按钮时,它应该显示多种颜色,我应该从中选择一种颜色,该颜色应该应用于button.How可以吗?需要您的建议朋友提前谢谢

EN

回答 3

Stack Overflow用户

发布于 2015-09-02 14:23:00

您需要创建自定义的可绘制XML并设置此按钮的背景。

android:background="@drawble/your XML"

你可以通过这种方式设置按钮按下时的颜色和正常状态的.Try,希望能对你有所帮助。

票数 0
EN

Stack Overflow用户

发布于 2015-09-02 15:08:15

试试这个..。

代码语言:javascript
复制
button.setOnClickListener(new OnClickListener() {

   void onClick(View v) {
   // create the color picker dialog and display it.
      ColorPickerDialog cDialog = new ColorPickerDialog(v.getContext(),new OnColorChangedListener()     
      {
          void colorChanged(int color) {
           mSelectedColor = color;
          }

     }),initialColor);

   cDialog.show();

   }
});

“mSelectedColor”将是应用于按钮的颜色。

票数 0
EN

Stack Overflow用户

发布于 2015-09-02 16:13:10

试着让这个希望对你有用..

在您想要显示颜色选项按钮的单击侦听器上调用colorDialog(),且body_view id被替换为您的按钮id。

colorDialog():-

代码语言:javascript
复制
private void colorDialog()
{
    LayoutInflater inflater = LayoutInflater.from(CreateNewNote.this);
    View view = inflater.inflate(R.layout.color_lay, null);

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(CreateNewNote.this);

    alertDialogBuilder.setTitle("Select Color");
    alertDialogBuilder.setIcon(R.drawable.alert_toast);
    alertDialogBuilder.setView(view);

    Button btncol1=(Button)view.findViewById(R.id.col1);
    Button btncol2=(Button)view.findViewById(R.id.col2);
    Button btncol3=(Button)view.findViewById(R.id.col3);
    Button btncol4=(Button)view.findViewById(R.id.col4);
    Button btncol5=(Button)view.findViewById(R.id.col5);
    Button btncol6=(Button)view.findViewById(R.id.col6);

    final AlertDialog alert = alertDialogBuilder.create();
    alert.show();

    title_lay=(RelativeLayout)findViewById(R.id.toplayout);
    body_view=(View)findViewById(R.id.body);


    btncol1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            body_view.setBackgroundResource(R.color.body_color1);
            alert.dismiss();
        }
    });

    btncol2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            body_view.setBackgroundResource(R.color.body_color2);
            alert.dismiss();
        }
    });
    btncol3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            body_view.setBackgroundResource(R.color.body_color3);
            alert.dismiss();
        }
    });
    btncol4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            body_view.setBackgroundResource(R.color.body_color4);
            alert.dismiss();
        }
    });
    btncol5.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            body_view.setBackgroundResource(R.color.body_color5);
            alert.dismiss();
        }
    });
    btncol6.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            body_view.setBackgroundResource(R.color.body_color6);
            alert.dismiss();
        }
    });

}

color_lay.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="10dp" >

    <Button
        android:id="@+id/col1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#F8BBD0" />

    <Button
        android:id="@+id/col2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_weight="1"
        android:background="#FFCCBC" />

    <Button
        android:id="@+id/col3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_weight="1"
        android:background="#E1BEE7" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp" >

    <Button
        android:id="@+id/col4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#BBDEFB" />

    <Button
        android:id="@+id/col5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_weight="1"
        android:background="#C8E6C9" />

    <Button
        android:id="@+id/col6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_weight="1"
        android:background="#FFF9C4" />

</LinearLayout>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32345687

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档