TextView不适合中心

时间:2014-01-08 08:47:29

标签: android android-layout textview

我动态地声明了一些水平布局,其中包含一些像ImageButtons,Buttons和TextViews这样的元素,这些元素是面向中心的,所有元素都是完美的,但是TextViews。我尝试直接像其他元素一样声明它们,并在垂直布局中声明它们,但两者都不起作用:

enter image description here

这是我的代码:

        // the layout params for the Horizontal Layout
        LinearLayout.LayoutParams lp_icon = new LinearLayout.LayoutParams(
              LinearLayout.LayoutParams.WRAP_CONTENT, 
              0, 1);
        lp_icon.setMargins(10, 15, 5, 0);

        // the layout params for the elements that contained in the horizontal LinearLayout
        LinearLayout.LayoutParams lp_ineer_ver = new LinearLayout.LayoutParams(
              LinearLayout.LayoutParams.MATCH_PARENT, 
              LinearLayout.LayoutParams.MATCH_PARENT, Gravity.CENTER);

        // the layout params for the TextViews
        LinearLayout.LayoutParams tests = new LinearLayout.LayoutParams(
              LinearLayout.LayoutParams.WRAP_CONTENT, 
              LinearLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER);

        // the layout params for the vertical layouts that contaion TextViews
        LinearLayout.LayoutParams temp_lay = new LinearLayout.LayoutParams(
              0, 
              LinearLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
            temp_lay.weight = 1;


        // the elements declaration
        icon1.setLayoutParams(lp_icon);// icon1 is then horizontal LinearLayout
        icon1.setBackgroundResource(R.drawable.ac_overlay);
        icon1.setOrientation(LinearLayout.HORIZONTAL);
        icon1.setTag(NORMAL);

        // TextView contained in a vertical Layout
        LinearLayout lay = new LinearLayout(this);
        icon1.addView(lay);
        lay.setLayoutParams(temp_lay);
        lay.setBackgroundColor(Color.TRANSPARENT);
        lay.setOrientation(LinearLayout.VERTICAL);
            TextView text1 = new TextView(this);
            lay.addView(text1);
            text1.setLayoutParams(tests);
            text1.setText("Master Bedroom");
            text1.setTextSize(12);
            text1.setTextColor(Color.WHITE);

        // other elements
        ImageButton image = new ImageButton(this);
        icon1.addView(image);
        image.setLayoutParams(lp_ineer_ver);
        image.setImageResource(R.drawable.grpbuttonfocus6);
        image.setBackgroundColor(Color.TRANSPARENT);

        Button testbut = new Button(this);
        icon1.addView(testbut);
        testbut.setLayoutParams(lp_ineer_ver);
        testbut.setText(" 8");
        testbut.setTextSize(12);
        testbut.setBackgroundColor(Color.TRANSPARENT);
        testbut.setTextColor(Color.WHITE);

        ImageButton testcol = new ImageButton(this);
        icon1.addView(testcol);
        testcol.setLayoutParams(lp_ineer_ver);
        testcol.setImageResource(R.drawable.home_cool);
        testcol.setBackgroundColor(Color.TRANSPARENT);

        // the TextView that's declared directly in the Horizontal Layout
        TextView text2 = new TextView(this);
        icon1.addView(text2);
        text2.setLayoutParams(lp_ineer_ver);
        text2.setText("00");
        text2.setTextSize(12);

3 个答案:

答案 0 :(得分:0)

textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);

您可以尝试添加以下代码以将布局参数应用于TextView

LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(LinearLayout.CENTER_IN_PARENT);
textView.setLayoutParams(lp);

答案 1 :(得分:0)

XML文件中的

android:gravity =“center”

答案 2 :(得分:0)

试试这个..

对于第一个textview icon1.addView(text1);和最后一个textview text2.setLayoutParams(tests);

,您的代码中有两个更改,如下所示

第一

        TextView text1 = new TextView(this);
        icon1.addView(text1);              //correction here
        text1.setLayoutParams(tests);
        text1.setText("Master Bedroom");
        text1.setTextSize(12);

和第二

    TextView text2 = new TextView(this);
    icon1.addView(text2);
    text2.setLayoutParams(tests);   //correction here
    text2.setText("00");
    text2.setTextSize(12);