我使用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和圖像視圖。但面臨着問題。
嘗試發佈您想要的結果的圖表或圖片,讓我們看看我是否可以幫助..但我建議去XML方式 – 2013-03-05 18:44:21