如何在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+"");

您可以设置父级重力(假设此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);在你包装视图时没有任何作用。

对文本视图使用Gravity属性

tv2.setGravity(中心);

tv2.setLayout_Gravity(中心);

我修复了该代码的工作

     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);

最新更新