RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 868135
Accepted
V.March
V.March
Asked:2020-08-13 21:39:54 +0000 UTC2020-08-13 21:39:54 +0000 UTC 2020-08-13 21:39:54 +0000 UTC

应用 Drawable 无法正常工作

  • 772

Drawable显示应用于按钮时遇到错误。
有 2linearLayout乘 2 Button。
每组只能选择一个按钮。在我应用的选定按钮上Drawable btnBackgroundOn。对未选中的——适当地应用Drawable btnBackgroundOff。

问题:如果它Activity失去焦点,然后恢复它,那么按钮的填充就变成了部分的。

在寻找原因时,我注意到以下几点:
1)按钮上填充空间的宽度变得等于所选按钮之一的宽度。
2)同组的按钮似乎不影响彼此的填充。
3)恢复后,如果第二组的按钮没有被触摸,我没有发现任何问题。

失去焦点之前:
在此处输入图像描述

焦点恢复后:
在此处输入图像描述

存储错误的最小代码:

主要活动:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button btn1a, btn1b, btn2a, btn2b;

    @BindViews({R.id.btn_1_a, R.id.btn_1_b})
    List<Button> listFirst;
    @BindViews({R.id.btn_2_a, R.id.btn_2_b})
    List<Button> listSecond;

    @BindDrawable(R.drawable.draw_color_on)
    Drawable btnBackgroundOn;
    @BindDrawable(R.drawable.draw_color_off)
    Drawable btnBackgroundOff;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ButterKnife.bind(this);

        btn1a = (Button) findViewById(R.id.btn_1_a);
        btn1b = (Button) findViewById(R.id.btn_1_b);
        btn2a = (Button) findViewById(R.id.btn_2_a);
        btn2b = (Button) findViewById(R.id.btn_2_b);

        btn1a.setOnClickListener(this);
        btn1b.setOnClickListener(this);
        btn2a.setOnClickListener(this);
        btn2b.setOnClickListener(this);
    }

    private void changeColorButton(List listBtn, Button curBtn) {

        for (int i = 0; i < listBtn.size(); i++) {

            if (listBtn.get(i) != curBtn) {
                ((Button) listBtn.get(i)).setBackground(btnBackgroundOff);

            } else {
                curBtn.setBackground(btnBackgroundOn);
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            //Block 1
            case R.id.btn_1_a:
                changeColorButton(listFirst, btn1a);
                break;
            case R.id.btn_1_b:
                changeColorButton(listFirst, btn1b);
                break;

            //Block 2
            case R.id.btn_2_a:
                changeColorButton(listSecond, btn2a);
                break;
            case R.id.btn_2_b:
                changeColorButton(listSecond, btn2b);
                break;
        }
    }
}

布局主:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff5cb"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/layout_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_1_a"
            android:layout_width="wrap_content"
            android:layout_height="32dp"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="8dp"
            android:layout_marginTop="8dp"
            android:background="@drawable/draw_color_off"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:text="First button text" />

        <Button
            android:id="@+id/btn_1_b"
            android:layout_width="wrap_content"
            android:layout_height="32dp"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="16dp"
            android:layout_marginTop="8dp"
            android:background="@drawable/draw_color_off"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:text="2-nd txt" />

    </LinearLayout>


    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_marginTop="16dp"
        android:background="#E0E0E0" />

    <LinearLayout
        android:id="@+id/layout_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_2_a"
            android:layout_width="wrap_content"
            android:layout_height="32dp"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="8dp"
            android:layout_marginTop="8dp"
            android:background="@drawable/draw_color_off"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:text="small" />

        <Button
            android:id="@+id/btn_2_b"
            android:layout_width="wrap_content"
            android:layout_height="32dp"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="16dp"
            android:layout_marginTop="8dp"
            android:background="@drawable/draw_color_off"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:text="Some text" />


    </LinearLayout>

</LinearLayout>

draw_color_on.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/colorPrimaryDark" />
    <corners android:radius="2dp" />
</shape>

draw_color_off.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FFFFFF" />
    <corners android:radius="2dp" />
</shape>

检查是否影响库ButterKnife- 不影响。
风格和表现没有特征。

我想如果我们为每个组制作自己的包,问题就可以解决Drawable。

我不能没有Drawable,因为我正在制作的应用程序有很多组按钮,但Drawable有几个错综复杂的层shape。

帮助我了解我在使用 `Drawable.

android
  • 2 2 个回答
  • 10 Views

2 个回答

  • Voted
  1. DroidCrafter
    2020-08-14T03:26:36Z2020-08-14T03:26:36Z

    你几乎是对的:

    我想如果每个组都有自己的 Drawable 包,问题就可以解决。

    是的,只是,您很可能必须为每个人都这样做Button。这肯定会很不方便。正如 Alexander Sobol 在这里
    所写,最简单的方法是立即将其应用到按钮上,方法是从资源中通过. DrawableContextCompat.getDrawable(this, <ВАШ_Drawable>)

    然后你的代码将是这样的:

    private void changeColorButton(List listBtn, Button curBtn) {
    
        for (int i = 0; i < listBtn.size(); i++) {
    
            if (listBtn.get(i) != curBtn) {
                ((Button) listBtn.get(i)).setBackground(ContextCompat.getDrawable(this, R.drawable.draw_color_off));
    
            } else {
                curBtn.setBackground(ContextCompat.getDrawable(this, R.drawable.draw_color_on));
            }
        }
    }
    
    • 2
  2. Best Answer
    Александр Соболь
    2020-08-14T00:11:04Z2020-08-14T00:11:04Z

    试着做

    setBackground(ContextCompat.getDrawable(R.drawable.draw_color_on))
    

    和

    setBackground(ContextCompat.getDrawable(R.drawable.draw_color_off))
    

    分别

    • 1

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    是否可以在 C++ 中继承类 <---> 结构?

    • 2 个回答
  • Marko Smith

    这种神经网络架构适合文本分类吗?

    • 1 个回答
  • Marko Smith

    为什么分配的工作方式不同?

    • 3 个回答
  • Marko Smith

    控制台中的光标坐标

    • 1 个回答
  • Marko Smith

    如何在 C++ 中删除类的实例?

    • 4 个回答
  • Marko Smith

    点是否属于线段的问题

    • 2 个回答
  • Marko Smith

    json结构错误

    • 1 个回答
  • Marko Smith

    ServiceWorker 中的“获取”事件

    • 1 个回答
  • Marko Smith

    c ++控制台应用程序exe文件[重复]

    • 1 个回答
  • Marko Smith

    按多列从sql表中选择

    • 1 个回答
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Suvitruf - Andrei Apanasik 什么是空? 2020-08-21 01:48:09 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5