平板电脑中的Android GUI问题与changedButton.getBackground()。setColorFilter(color,mode);

时间:2016-01-05 21:23:37

标签: android android-layout

如何在标题中说,我的应用程序在智能手机(三星S2和三星S3)中完美运行,但平板电脑中存在GUI错误(三星Galaxy Tab S与Android 5.0.2)。

用户可以更改此活动。 他可以触摸一个按钮并像这样更改文字和颜色

enter image description here

但是在平板电脑中如果用户切换方向或关闭并打开活动,有时会出现此问题!

enter image description here

所有Button s采用第一个输入颜色的颜色! 如果用户交换方向或关闭并重新打开Activity,一切都会恢复正常状态!

此处代码更改了用户过去更改的所有Button

static String clickedButtonViewId;
private List<MaterieVoti> materie;
MySQLiteHelper db = new MySQLiteHelper(this);

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

//Get all materie inside database
     List<Materia> materia = db.getAllMaterie();
     //change all TextView inputed from user
     if(materia.isEmpty()){
         //do nothing
     }else {
         for (Materia mat : materia) {
             //Change all the Button with values stored inside the database
             int resId = getResources().getIdentifier(mat.getID(), "id", getPackageName());
             Button changedButton = (Button) findViewById(resId);
             changedButton.setText(mat.getMateria());
             changedButton.setTypeface(null, Typeface.BOLD);
             changedButton.getBackground().setColorFilter(mat.getColor(), PorterDuff.Mode.MULTIPLY);

         }
     }
 }

//Take back data from ActivityAddMateria
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == 1) {
            if (resultCode == RESULT_OK) {

                MySQLiteHelper db = new MySQLiteHelper(this);
                String result = data.getStringExtra("result"); //Take the materia from Dialog
                int color = data.getIntExtra("color", 1); //Take the color from Dialog

                //Controllo se il Button è già presente nel db se presente aggiorno se non presente inserisco
                boolean modifica = db.Exists(clickedButtonViewId);

                if(!modifica) {

                    //Materia da inserire in un nuovo spazio
                    db.addMateriaToDb(new Materia(clickedButtonViewId, result, color));

                }else{

                    //Materia già presente nel Button quindi aggiorno la materia
                    db.updateMateria(new Materia(clickedButtonViewId, result, color));
                    Toast.makeText(getApplicationContext(), "Materia modificata!",
                            Toast.LENGTH_LONG).show();
                }

                //Cambio subito il Button
                int resId = getResources().getIdentifier(clickedButtonViewId, "id", getPackageName());
                final Button clickedtextView = (Button) findViewById(resId);

                clickedtextView.setText(result);
                clickedtextView.getBackground().setColorFilter(color, PorterDuff.Mode.MULTIPLY);
            }

            if (resultCode == RESULT_CANCELED) {
                //Nessuna materia inserita
            }

        }
    }//onActivityResult

抽拉/ buttons.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/button_pressed"
        android:state_pressed="true" />

    <item android:drawable="@drawable/button_focused"
        android:state_focused="true" />

    <item android:drawable="@drawable/button_default" />

</selector>

抽拉/ button_default.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <corners
        android:radius="100dp"
        />
    <solid
        android:color="#FFFFFF"
        />
    <padding
        android:left="0dp"
        android:top="0dp"
        android:right="0dp"
        android:bottom="0dp"
        />
    <stroke
        android:width="3dp"
        android:color="#787878"
        />
</shape>

我发现了问题,但不知道为什么它不起作用。 如果我用changedButton.getBackground().setColorFilter(mat.getColor(), PorterDuff.Mode.MULTIPLY);更改changedButton.setBackgroundColor(mat.getColor());,问题就会消失,但我会丢失按钮样式属性!

一种方法是创建一个drawable / button_style_red.xml,drawable / button_style_yellow.xml,并使用代码Button.setDrawable(button_style_color);我可以更改Button但是有一些方法只能改变颜色而不改变Button s的风格

Button.getBackground().setColorFilter(mat.getColor(), PorterDuff.Mode.MULTIPLY);对所有设备都不起作用!!

这是我的旧问题,使更改的按钮具有相同的样式link

0 个答案:

没有答案