0

在我的代码中,我想在 CENTER 中设置文本视图,请参阅此代码,告诉我如何设置

我尝试编辑此代码,但日期文本视图未出现 CENTER 请告诉更改

    LinearLayout ll2=new LinearLayout(ctx);
    LayoutParams lp3=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    ll2.setLayoutParams(lp3);
     ll2.setOrientation(LinearLayout.VERTICAL);

     LayoutParams lp2=null;
     LinearLayout ll4=new LinearLayout(ctx);
     lp2=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

     TextView tv2=new TextView(ctx);
     lp2=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.FILL_PARENT);

    tv2.setLayoutParams(lp2);
    tv2.setText("MESSAGE");
    tv2.setBackgroundResource(R.drawable.rec);
     tv2.setTextSize(14);
     tv2.setTextColor(Color.parseColor("#5A3084"));
    if(sm!=null && sm.message!=null)
        tv2.setText(sm.message.toString());
    tv2.setGravity(Gravity.LEFT);
    tv2.setPadding(10, 0, 10, 0);

    TextView tv0=new TextView(ctx);
lp2=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    tv0.setLayoutParams(lp2);
    tv0.setText("Alertdate :");
    tv0.setBackgroundResource(R.drawable.rec1);
    tv0.setTextColor(Color.parseColor("#38385E"));
    tv0.setGravity(Gravity.CENTER_VERTICAL);




     tv0.setTextSize(12);
    if(sm!=null && sm.alertdate!=null)

        tv0.setText(sm.alertdate+"");
4

3 回答 3

3

您可以设置父母的重力(假设这个 TextViews 父母是ll4

LinearLayout ll4 = new LinearLayout(ctx);
// ... other methods
ll4.setGravity(Gravity.CENTER_VERTICAL);

并且不要忘记将您的孩子添加到其中

TextView tv0 = new TextView(ctx);
// ... other methods
ll4.addView(tv0);

我认为tv0.setGravity(Gravity.CENTER_VERTICAL);当您包装视图时这不会做任何事情。

于 2013-07-08T13:34:29.593 回答
0

对 textview 使用 Gravity 属性

tv2.setGravity(中心);

tv2.setLayout_Gravity(CENTER);

于 2013-07-08T13:20:38.277 回答
-1

我修复了这个代码它的工作

     LinearLayout ll2 = new LinearLayout(ctx);
    LayoutParams lp3 = new LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT);
    ll2.setLayoutParams(lp3);
    ll2.setOrientation(LinearLayout.VERTICAL);

    LayoutParams lp2 = null;
    LinearLayout ll4 = new LinearLayout(ctx);
    lp2 = new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);

    TextView tv2 = new TextView(ctx);
    lp2 = new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.FILL_PARENT);

    tv2.setLayoutParams(lp2);
    tv2.setText("MESSAGE");
    tv2.setBackgroundResource(R.drawable.rec3);
    tv2.setTextSize(14);
    // tv2.setTextColor(Color.parseColor("#ffffff"));
    tv2.setTextColor(Color.parseColor("#5A3084"));
    Typeface typface = Typeface.createFromAsset(ctx.getAssets(),
            "fonts/Roboto-Regular.ttf");

    tv2.setTypeface(typface);
    if (sm != null && sm.message != null)
        tv2.setText(sm.message.toString());
    tv2.setGravity(Gravity.LEFT);
    tv2.setPadding(15, 8, 10, 8);

    TextView tv0 = new TextView(ctx);
    LinearLayout.LayoutParams lp21 = new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    lp21.gravity = Gravity.CENTER;

    tv0.setLayoutParams(lp21);
    tv0.setText("Alertdate :");
    tv0.setBackgroundResource(R.drawable.rec1);
    tv0.setTextColor(Color.parseColor("#ffffff"));

    tv0.setTextSize(12);
    if (sm != null && sm.alertdate != null)
        tv0.setGravity(Gravity.CENTER_VERTICAL);
    dataF = sm.alertdate;
    outdat = changeDateFormat(dataF);
    tv0.setText(outdat);

    // tv0.setText(sm.alertdate+"");

    tv0.setPadding(10, 0, 10, 0);

    // tv0.setBackgroundResource(R.drawable.custom);
    TextView tv1 = new TextView(ctx);
    lp2 = new LayoutParams(pixelCount(10), pixelCount(10));
    tv1.setLayoutParams(lp2);
于 2013-08-13T03:39:58.557 回答