2013-03-05 104 views
0

我使用2個textview,4個按鈕,1個seekbar,1個圖像視圖的線性佈局。如果我將這些文本瀏覽,按鈕等放置在線性佈局中,則Android手機中的對齊方式很好。雖然我在Android平板電腦上運行相同的代碼,但對齊方式並不合適。爲什麼這種對齊方式在平板電腦中不適用?我通過java代碼創建了textviews,buttons等。即使我通過設置第二個文本視圖的左邊距設備寬度/ 2在Android手機和平板電腦中具有差異來水平指定兩個文本視圖。我需要像下面一樣對齊。在android中的對齊線性佈局

TextView1       TextView2 
Button1 Button2 Button3 Button4  SeekBar ImageView 

這是我的代碼。

LinearLayout.LayoutParams textViewParams1 = new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
    textViewLayout.setOrientation(LinearLayout.HORIZONTAL); 
    TextView TextView1=new TextView(this); 
    TextView1.setText("Text1"); 
    textViewParams1.gravity=Gravity.CENTER; 
    textViewParams1.setMargins(60, 20, 40, 10); 
    textViewLayout.addView(chooseColorTextView, textViewParams1); 

    LinearLayout.LayoutParams textViewParams2 = new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
    TextView TextView2=new TextView(this); 
    TextView2.setText("Text2"); 
    int width=getWindowManager().getDefaultDisplay().getWidth(); 
    textViewParams2.gravity=Gravity.CENTER;  
    textViewParams2.setMargins((width/2), 20, 40, 10); 
    textViewLayout.addView(strokeWidthTextView, textViewParams2); 

    parentlinearLayout.addView(textViewLayout, textViewLayoutParams); 

在下一個線性佈局中,我添加了4個按鈕,seekbar和圖像視圖。但面臨着問題。

+1

嘗試發佈您想要的結果的圖表或圖片,讓我們看看我是否可以幫助..但我建議去XML方式 – 2013-03-05 18:44:21

回答

0

請閱讀更多關於wrap_content和其他android控件的信息。還閱讀關於平板電腦的dpi。由於分辨率的外觀變化。

2

我建議創建複雜的佈局,必須在XML中以不同的屏幕尺寸呈現,而不是以編程方式呈現,因此您可以在res/layout和res/layout-large中擁有兩個不同的main.xml,系統會拾取正確的取決於屏幕尺寸。

0

爲什麼不使用TableLayout?它是您需要管理單元格對齊的單元格:

您需要的全部內容是在span屬性中使單元格使用多列。

TableLayout myLayout = new TableLayout(myContext); 

TableRow row1 = new TableRow(myContext); 
TableRow.LayoutParams layouparams1 = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.WRAP_CONTENT); 
layouparams1.span = 4; 
row1.setLayoutParams = layouparams1 ; 

TableRow.LayoutParams layouparams2 = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.WRAP_CONTENT); 
layouparams2.span = 2; 
TableRow row2 = new TableRow(myContext); 
row2.setLayoutParams = layouparams2 ; 


//then create and add your childs to each row : 
... 
row1.addView(text1); 
row1.addView(text1); 

row1.addView(button1); 
row1.addView(button2); 
row1.addView(button3); 
row1.addView(button4); 
row1.addView(seekbar); 
row1.addView(imageView); 

myLayout.addView(row1); 
myLayout.addView(row2); 

也考慮加入layout_weight兒童管理他們離開彼此的空間。

1

使用layout_weight和weightSum在XML:

<LinearLayout android:weightSum="1" android:layout_width="match_parent" android:layout_height="wrap_content"> 
    <!-- First column --> 
    <LinearLayout android:layout_weight=".5" android:layout_width="0dp" android:layout_height="wrap_content"> ... </LinearLayout> 

    <!-- Second column --> 
    <LinearLayout android:layout_weight=".5" android:layout_width="0dp" android:layout_height="wrap_content"> ... </LinearLayout> 
</LinearLayout> 

這將產生一個動態調整2列布局。如果你想分割更短或更長,改變.5爲.3和.7爲30/70%拆分等。

+0

非常感謝安德魯。它工作正常。 – Karthick 2013-03-06 17:58:59