Android 运行时编程生成的复选框未在所有屏幕中设置为正确

Android 运行时编程生成的复选框未在所有屏幕中设置为正确,android,android-layout,checkbox,android-checkbox,Android,Android Layout,Checkbox,Android Checkbox,我正在从数据库中获取复选框值。并且在语法上做了复选框…它运行良好,复选框已经制作好 实际上,我面临的一个问题是,复选框在所有屏幕上看起来都不一样 我该怎么办 这是我的xml代码:- <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_p

我正在从数据库中获取复选框值。并且在语法上做了复选框…它运行良好,复选框已经制作好

实际上,我面临的一个问题是,复选框在所有屏幕上看起来都不一样

我该怎么办

这是我的xml代码:-

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

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:id="@+id/linearLayoutMain"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    </LinearLayout>
  </HorizontalScrollView>

  </LinearLayout>

这是我的活动:-

 public class Amardeep extends Activity {
 String responseString, success, name, id, pref, per_amout, percentage,
        datediff, dateNo, minpvalue, discountonit, bus_id, bus_type_id,
        str;
 int a;
String[] idsplit, namesplit, prefsplit;
List<String> testArrayList;
LinearLayout llmain;
LinearLayout[] lLayout;
Integer count1 = 0;
Context mContext;
CheckBox cb;
CheckBox[] cbs;
ArrayList<String> list = new ArrayList<String>();
StringBuffer result = new StringBuffer();
String finalresult, emp_access_name, minredemption, min_redeem;
Integer count = 0;
String data = "";
Button save;
TextView tvo;
EditText mpv, discount, mra;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.amardeep);

    new Homedata1().execute();

    llmain = (LinearLayout) findViewById(R.id.linearLayoutMain);
}

   class Homedata1 extends AsyncTask<String, String, String> {
    private ProgressDialog pDialog;

    protected void onPreExecute() {
        pDialog = new ProgressDialog(Amardeep.this);
        pDialog.setMessage("Getting Data ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.setCanceledOnTouchOutside(false);
        pDialog.show();
        super.onPreExecute();
    }

    protected String doInBackground(String... params) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(
                "http://www.xyz.in/android_new/checkbox.php");
        try {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                    2);
            nameValuePairs.add(new BasicNameValuePair("bus_id", "B115"));
            nameValuePairs.add(new BasicNameValuePair("bus_type_id",
                    "BT101"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            response.getStatusLine().getStatusCode();
            HttpEntity getResponseEntity = response.getEntity();
            responseString = EntityUtils.toString(getResponseEntity);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
        return responseString;
    }

    @SuppressLint("NewApi")
    protected void onPostExecute(String resultStr) {
        try {
            JSONObject json = new JSONObject(responseString);
            JSONArray jArray = json.getJSONArray("customer");
            for (int i = 0; i < jArray.length(); i++) {
                JSONObject json_data = jArray.getJSONObject(i);
                success = json_data.getString("success");
                id = json_data.getString("id");
                name = json_data.getString("name");
                pref = json_data.getString("pref");

                idsplit = id.split(",");
                a = idsplit.length;
                namesplit = name.split(",");
                prefsplit = pref.split(",");
                testArrayList = new ArrayList<String>(
                        Arrays.asList(prefsplit));
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (success.equals("1")) {
            int b = (a / 3);
            int c = (a % 3);
            if (c != 0) {
                b = b + 1;
            }
            lLayout = new LinearLayout[b];
            cbs = new CheckBox[a];

            for (int j = 0; j < b; j++) {
                int x = 0;
                x = x + (j * 3);
                lLayout[j] = new LinearLayout(Amardeep.this);
                llmain.addView(lLayout[j]);
                for (int i = x; i < x + 3; i++) {
                    cbs[i] = new CheckBox(Amardeep.this);
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                            140, ViewGroup.LayoutParams.WRAP_CONTENT, 1);                   
                    params.leftMargin = 15;
                    params.topMargin = 10;
                    if ((i + 1) > a) {
                        break;
                    } else {
                        if (testArrayList.contains(idsplit[i])) {

                            cbs[i].setLayoutParams(params);
                            cbs[i].setText(namesplit[i]);
                            cbs[i].setId(i + 1);
                            cbs[i].setChecked(true);
                            count++;
                            result.append(i + 1 + ",");
                            cbs[i].setTextColor(Color.BLACK);
                            cbs[i].setTextSize(10f);
                            cbs[i].setButtonDrawable(R.drawable.checkbox);
                            cbs[i].setPadding(35, 0, 30, 0);
                            cbs[i].setTag(i + 1);
                            if (count1.equals(1)) {
                                cbs[i].setEnabled(true);
                            } else {
                                cbs[i].setEnabled(false);
                            }
                            lLayout[j].addView(cbs[i]);
                        } else {

                            cbs[i].setLayoutParams(params);
                            cbs[i].setText(namesplit[i]);
                            cbs[i].setId(i + 1);
                            cbs[i].setTextColor(Color.BLACK);
                            cbs[i].setTextSize(10f);
                            cbs[i].setButtonDrawable(R.drawable.checkbox);
                            cbs[i].setPadding(35, 0, 30, 0);
                            cbs[i].setTag(i + 1);
                            if ((count1.equals(1))) {
                                cbs[i].setEnabled(true);
                            } else {
                                cbs[i].setEnabled(false);
                            }
                            lLayout[j].addView(cbs[i]);
                        }
                    }
                }
            }
        } else {
            Toast toast = Toast.makeText(getApplicationContext(),
                    "Data empty", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        }
        pDialog.dismiss();
    }
}
}
公共类活动{
字符串响应字符串、成功、名称、id、优先级、百分比、,
datediff、dateNo、minpvalue、折扣单元、总线id、总线类型id、,
str;
INTA;
字符串[]idsplit,namesplit,prefsplit;
列出测试列表;
线性布局;
直线布局[]lLayout;
整数count1=0;
语境;
复选框cb;
复选框[]cbs;
ArrayList=新建ArrayList();
StringBuffer结果=新的StringBuffer();
字符串finalresult、emp\u access\u name、minredemption、minredemption;
整数计数=0;
字符串数据=”;
按钮保存;
TextView-tvo;
EditText mpv、折扣、mra;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.amardeep);
新建Homedata1().execute();
llmain=(LinearLayout)findViewById(R.id.linearLayoutMain);
}
类Homedata1扩展了异步任务{
私人对话;
受保护的void onPreExecute(){
pDialog=newprogressdialog(amardeph.this);
setMessage(“获取数据…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(假);
pDialog.setCanceledOnTouchOut(假);
pDialog.show();
super.onPreExecute();
}
受保护的字符串doInBackground(字符串…参数){
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(
"http://www.xyz.in/android_new/checkbox.php");
试一试{
List name valuepairs=new ArrayList(
2);
添加(新的BasicNameValuePair(“总线id”,“B115”));
添加(新的BasicNameValuePair(“总线类型id”),
“BT101”);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
response.getStatusLine().getStatusCode();
HttpEntity getResponseEntity=response.getEntity();
responseString=EntityUtils.toString(getResponseEntity);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
}捕获(IOE异常){
//TODO自动生成的捕捉块
}
回报率;
}
@SuppressLint(“新API”)
受保护的void onPostExecute(字符串resultStr){
试一试{
JSONObject json=新的JSONObject(responseString);
JSONArray jArray=json.getJSONArray(“客户”);
for(int i=0;ia){
打破
}否则{
if(testArrayList.contains(idsplit[i])){
cbs[i].setLayoutParams(params);
cbs[i].setText(namesplit[i]);
cbs[i].setId(i+1);
cbs[i].setChecked(true);
计数++;
结果.追加(i+1+“,”);
cbs[i].setTextColor(Color.BLACK);
cbs[i].setTextSize(10f);
cbs[i].setButtonDrawable(R.drawable.checkbox);
cbs[i].setPadding(35,0,30,0);
cbs[i].setTag(i+1);
if(count1.等于(1)){
cbs[i].setEnabled(真);
}否则{
cbs[i].setEnabled(false);
}
lLayout[j].addView(cbs[i]);
}否则{
cbs[i].setLayoutParams(params);
cbs[i].setText(namesplit[i]);
cbs[i].setId(i+1);
cbs[i].setTextColor(Color.BLACK);
cbs[i].setTextSize(10f);
cbs[i].setButtonDrawable(R.drawable.che
res/layout/my_layout.xml             // layout for normal screen size ("default")
res/layout-small/my_layout.xml       // layout for small screen size
res/layout-large/my_layout.xml       // layout for large screen size
res/layout-xlarge/my_layout.xml      // layout for extra large screen size
res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation

res/drawable-mdpi/my_icon.png        // bitmap for medium density
res/drawable-hdpi/my_icon.png        // bitmap for high density
res/drawable-xhdpi/my_icon.png       // bitmap for extra high density
public class CustomCheckBox extends CheckBox {

    private Context mContext;

    public CustomCheckBox(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        sharedConstrutor(context);
    }

    public CustomCheckBox(Context context, AttributeSet attrs) {
        super(context, attrs);
        sharedConstrutor(context);
    }

    public CustomCheckBox(Context context) {
        super(context);
        sharedConstrutor(context);
    }

    public void sharedConstrutor(Context context) {
        this.mContext=context;
        if(!isInEditMode())
            setTypeface(Typeface.createFromAsset(mContext.getAssets(), "fonts/MuseoSans_300.ttf"));

//set other values as needed here.
    }
}
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.leftMargin = 123;
li.addView(cb, params);