简体   繁体   中英

Android Checkbox Border Disappears Using Java

I'm making an Android app which has checkboxes in. I added in a few sample things using the XML files just to get the layout looking how I want it. When I put a checkbox in using the XML file, it has a border around it, but when I use Java to add the checkbox programmatically, it gets rid of the border around it, making it almost impossible to see the checkbox.

What I want to know is how do I go about formatting the checkbox to look the same as the XML style? Can it be done using LayoutParams or is there some other way of doing it? I've read about setButtonDrawable or something but have no idea how to go about it, can someone point me to a basic tutorial if this is the way to go?

XML Checkbox xml checkbox image http://redsquirrelsoftware.co.uk/xml.jpg

Java Checkbox java checkbox image http://redsquirrelsoftware.co.uk/java.jpg

XML Code

<CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingRight="30dp"
        android:text="@string/checkbox" />

Java Code:

 CheckBox checkbox = new CheckBox(this);
    checkbox.setText(R.string.checkbox);
    checkbox.setTextSize(TypedValue.COMPLEX_UNIT_PT, 10);
    checkbox.setTextColor(Color.BLACK);

How about adding padding? checkbox.setPadding(0,0,30,0);

I tried this and works, maybe it's because you didn't set LayoutParams

CheckBox checkbox = new CheckBox(this);
checkbox.setText("completed");
checkbox.setTextSize(TypedValue.COMPLEX_UNIT_PT, 10);
checkbox.setTextColor(Color.BLACK);
checkbox.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

LinearLayout layout = (LinearLayout)findViewById(R.id.layoutku);
layout.addView(checkbox);

Ok, it is actually going to the standard Checkbox styling used in the XML file, but only when I close the app and re-open it again. I'm guessing this may be because I'm running it via USB from Eclipse in developer mode.

Will test it to see properly once I've uploaded it to the Play Store and see if it works as it's supposed to then. Not the biggest issue in the world if the app does have to be re-opened before it displays properly though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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