我尝试将TextView添加到垂直LinearLayout并将文本视图对齐到布局右侧:
LinearLayout temprLayout=new LinearLayout(this);
temprLayout.setOrientation(LinearLayout.VERTICAL);
theTemprature = new TextView(this);
theTemprature.setVisibility(View.VISIBLE);
theTemprature.setTextSize(21);
theTemprature.setTextColor(0xffffCC33);
theUVText = new TextView(this);
theUVText.setVisibility(View.VISIBLE);
theUVText.setTextSize(21);
theUVText.setTextColor(0xfff5b800);
theUVText.setBackgroundColor(0xff423234);
theUVText.setGravity(Gravity.RIGHT | Gravity.TOP);
theUVText.setLayoutParams(new FrameLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
Gravity.RIGHT));
temprLayout.addView(theTemprature, new FrameLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
Gravity.RIGHT));
temprLayout.addView(theUVText, new FrameLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
Gravity.RIGHT));
但是textview仍然与布局左侧对齐
答案 0 :(得分:4)
尝试将LinearLayout设置为MATCH_PARENT宽度。