在我的代码中,我想在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+"");
答案 0 :(得分: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);
没有做任何事情。
答案 1 :(得分:0)
对textview使用Gravity属性
tv2.setGravity(中心);
tv2.setLayout_Gravity(CENTER);
答案 2 :(得分:-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);