2012-01-13 37 views
0

我需要一個視圖像這個TextView | TextView的問題是我想要的內容是垂直n等於兩邊。像50/50的屏幕去每個視圖(我正在編程)。任何建議...謝謝要找出是否有可能對齊兩個文本視圖

+0

發佈您的代碼。 – 2012-01-13 10:58:21

回答

1

使用

android:layout_weight="1" 

兩個TextView的

+0

我正在以編程方式生成它。 – Arun 2012-01-13 10:52:55

+0

android.view.Display display =((android.view.WindowManager)getSystemService(Context.WINDOW_SERVICE))。getDefaultDisplay(); txtview.setWidth((int)(display.getWidth()/ 2)) – Arun 2012-01-13 11:03:52

+1

更好的選擇我發現它..! – Arun 2012-01-13 11:04:15

0

使用LinearLayout具有用於每個文本的意見相同值的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的權重,可以將其設置爲你想要的,只要它是相同的值)

+0

它不工作先生。至少不是在我的情況。 – Arun 2012-01-13 10:56:19

+0

然後發佈您的佈局設置,以便我們可以看到問題所在。因爲設定重量是你想要做的事情的答案。可能在您的佈局中的另一個設置阻止它的工作。 – Guillaume 2012-01-13 10:59:32

+0

android.view.Display display =((android.view.WindowManager)getSystemService(Context.WINDOW_SERVICE))。getDefaultDisplay(); btn.setWidth((int)(display.getWidth()/ 2)); – Arun 2012-01-13 11:04:20

0

XML版本(使用TableLayout,招工程的LinearLayout此外,如果你喜歡這些):

<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:

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)); 
相關問題