我正在尝试对齐 n 个文本视图,其中可以有不同数量的垂直和水平居中的行,并且具有更多行的文本视图“下降”,即使它们具有固定的大小和中心对齐方式。是否可以通过代码对齐它们?
这是我的xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one line"
android:textSize="48dp"
tools:context=".MainActivity" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two lines"
android:textSize="48dp" />
</LinearLayout>
这是我的java类:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView oneline=(TextView) findViewById(R.id.textView1);
TextView twolines=(TextView) findViewById(R.id.textView2);
twolines.setText("two lines \n two lines");
oneline.setHeight(200);
twolines.setHeight(200);
oneline.setWidth(400);
twolines.setWidth(400);
oneline.setBackgroundColor(Color.YELLOW);
twolines.setBackgroundColor(Color.YELLOW);
oneline.setGravity(Gravity.CENTER);
twolines.setGravity(Gravity.CENTER);
}
这就是结果:-\