Android 查找是否为';可以将两个文本视图特别对齐

Android 查找是否为';可以将两个文本视图特别对齐,android,android-layout,Android,Android Layout,我需要这样一个视图text view | text view问题是我希望里面的内容垂直,两边相等。。就像50/50的屏幕进入每个视图(我是通过编程实现的)。任何建议。。。谢谢使用 android:layout_weight="1" 对于两个文本视图使用线性布局,每个文本视图的android:weight具有相同的值,这应该可以做到。比如: textView1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONT

我需要这样一个视图text view | text view问题是我希望里面的内容垂直,两边相等。。就像50/50的屏幕进入每个视图(我是通过编程实现的)。任何建议。。。谢谢使用

android:layout_weight="1"

对于两个文本视图

使用
线性布局
,每个文本视图的
android:weight
具有相同的值,这应该可以做到。比如:

textView1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0.5));
textView2.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0.5));
(最后一个设置为0.5的参数是权重,您可以将其设置为所需的值,只要相同)

XML版本(使用TableLayout,如果您喜欢,技巧也适用于LinearLayout):


我以编程方式生成它。android.view.Display Display=((android.view.WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();然后将布局设置发布到txtview.setWidth((int)(display.getWidth()/2)),这样我们就可以看到问题所在。因为设置权重是你想要做的事情的答案。布局中的另一个设置可能会阻止它工作。android.view.Display Display=((android.view.WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();btn.setWidth((int)(display.getWidth()/2));这是很好的工作。我在某个地方发现了它。我认为,对于你的努力来说,编程工作比X更好
<TableLayout android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <TableRow>
        <!-- Set the width to 0dp and set layout_weight=1! on both Views-->
        <TextView
            android:text="This is text1, its pretty long but that shouldn't be a problem"
            android:layout_marginLeft="1px"
            android:background="#ff0000"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />

        <TextView
            android:text="Shorter"
            android:background="#00ff00"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />
    </TableRow>
</TableLayout>
    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.HORIZONTAL);

    TextView t1 = new TextView(this);
    t1.setBackgroundColor(Color.BLUE);
    t1.setText("This is text1, its pretty long but that shouldn't be a problem");
    TextView t2 = new TextView(this);
    t2.setBackgroundColor(Color.GRAY);
    t2.setText("Shorter");

    layout.addView(t1, new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f));
    layout.addView(t2, new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f));