简体   繁体   English

AppCompatCheckBox不适用于以下API 21

[英]AppCompatCheckBox not working for below API 21

I created a dynamic checkbox with the following code: 我使用以下代码创建了一个动态复选框:

xml: XML:

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


</LinearLayout>

java: Java的:

LinearLayout ll = (LinearLayout) findViewById(R.id.layout_checkbox);

ll.removeAllViews();

for (int i = 0; i < 10; i++) {

    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    AppCompatCheckBox myCheckBox = new AppCompatCheckBox(getApplicationContext());
    myCheckBox.setText(i);
    myCheckBox.setTextColor(Color.parseColor("#FFFFFF"));
    myCheckBox.setHintTextColor(Color.parseColor("#FFFFFF"));
    myCheckBox.setTextSize(12);

    myCheckBox.setId(i);

    ll.addView(myCheckBox, lp);
}

Now from above code only LOLLIPOP version shows the checkbox with text. 现在从上面的代码中只有LOLLIPOP版本显示带有文本的复选框。 And for below LOLLIPOP version it shows only Text but not showing the checkbox. 对于以下LOLLIPOP版本,它仅显示Text但不显示复选框。

Same thing work with all device if I put the below code in xml file: 如果我将以下代码放在xml文件中,同样适用于所有设备:

<android.support.v7.widget.AppCompatCheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Testing"
    android:buttonTint="@color/colorAccent"/>

But I can't define the checkbox in xml as I have to create it dynamically. 但我不能在xml中定义复选框,因为我必须动态创建它。

Even setButtonTintList is not working for below LOLLIPOP 甚至setButtonTintList也不适用于LOLLIPOP

How can I show the Checkbox for below LOLLIPOP version with AppCompatCheckBox ? 如何使用AppCompatCheckBox显示LOLLIPOP版本下方的Checkbox?

Do not use getApplicationContext() , for the Context passed in to new AppCompatCheckBox() you need to use a reference to your activity (that extends AppCompatActivity ) context for it to correctly inject the styling for the AppCompatCheckBox . 不要使用getApplicationContext() ,对于传递给new AppCompatCheckBox()Context ,您需要使用对您的活动(扩展AppCompatActivity )上下文的引用来正确地为AppCompatCheckBox注入样式。 This will be new AppCompatCheckBox(this) if you're creating this in an activity, or new AppCompatCheckBox(getActivity()) if you're creating this in a fragment. 如果你在一个活动中创建它,那么这将是new AppCompatCheckBox(this)如果你在一个片段中创建它,那么它将是new AppCompatCheckBox(this) new AppCompatCheckBox(getActivity())

Code like this below will work on all versions: 下面的代码适用于所有版本:

public class MainActivity extends AppCompatActivity {

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

    LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
    for (int i = 0; i < 10; i++) {
      LinearLayout.LayoutParams lp =
          new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

      AppCompatCheckBox myCheckBox = new AppCompatCheckBox(this);
      myCheckBox.setText("text");
      myCheckBox.setTextSize(12);

      myCheckBox.setId(i);

      layout.addView(myCheckBox, lp);
    }
  }

}

This will definitely work 这肯定会奏效

 <android.support.v7.widget.AppCompatCheckBox
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Testing"
                app:buttonTint="@color/colorAccent"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM