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.


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