我是新來的Android &這個優秀的問題/答案的網站..安卓:線性佈局之間的水平差距
目前我面臨的一個問題,內用線性佈局之間間隙/空間。
我有多個線性佈局嵌套(線性佈局 - >滾動視圖佈局 - >多個線性佈局)。
我的目標是顯示ImageView(與圖像)大小根據設備屏幕分辨率動態更改。
每個內部佈局都有ImageView控件。我寫了調整每個ImageView在運行時適合屏幕。在設計時,內部線性佈局顯示爲沒有橫向間隙,但是在朗姆酒時間由於動態代碼,它們是線性佈局之間的差距。
Design display
____________ ____________
| | | |
| | | |
| ICON - 1 | | ICON - 2 |
| | | |
____________ ____________
NO horizontal space
____________ ____________
| | | |
| | | |
| ICON - 3 | | ICON - 4 |
| | | |
____________ ____________
Runtime display
____________ ____________
| | | |
| | | |
| ICON - 1 | | ICON - 2 |
| | | |
____________ ____________
un wanted horizontal space
____________ ____________
| | | |
| | | |
| ICON - 3 | | ICON - 4 |
| | | |
____________ ____________
的main.xml代碼
<LinearLayout
android:background="@drawable/llrightborder"
android:id="@+id/mainlayoutLeft"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="vertical" >
<ScrollView android:id="@+id/scrollView1" android:layout_width="fill_parent" android:layout_height="wrap_content">
<LinearLayout android:orientation="vertical" android:paddingTop="10.0dip" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginBottom="2dip">
<LinearLayout
android:id="@+id/leftRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<ImageView
android:id="@+id/imageicon1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon1"
android:layout_marginRight="2dip"
android:clickable="true" />
<ImageView
android:id="@+id/imageicon2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon2"
android:layout_marginLeft="2dip"
android:clickable="true" />
</LinearLayout>
<LinearLayout android:id="@+id/leftRow2" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content">
<ImageView
android:id="@+id/imageicon3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon3"
android:layout_marginRight="2dip"
android:clickable="true" />
<ImageView
android:id="@+id/imageicon4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon4"
android:layout_marginLeft="2dip"
android:clickable="true" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
main.java用於顯示圖像尺寸代碼動態
DisplayMetrics metrics = getResources().getDisplayMetrics();
iHeight = metrics.heightPixels;
iWidth = metrics.widthPixels;
leftLLWidth = (int) ((iWidth * 25)/100);
rightLLWidth = (int) (iWidth - leftLLWidth);
leftLL = (LinearLayout) findViewById(R.id.mainlayoutLeft);
rightLL = (LinearLayout) findViewById(R.id.mainlayoutRight);
leftLL.setLayoutParams(new LinearLayout.LayoutParams(leftLLWidth, iHeight));
rightLL.setLayoutParams(new LinearLayout.LayoutParams(rightLLWidth, iHeight));
//init image buttons
img_icon1 = (ImageView) findViewById(R.id.imageicon1);
img_icon2 = (ImageView) findViewById(R.id.imageicon2);
img_icon3 = (ImageView) findViewById(R.id.imageicon3);
img_icon4 = (ImageView) findViewById(R.id.imageicon4);
btnWidth = (int) ((leftLLWidth/2) - 3);
btnHeight = btnWidth + 60;
ViewGroup.LayoutParams button_params = img_icon1.getLayoutParams();
button_params.width = btnWidth;
button_params.height = btnHeight;
img_icon1.setLayoutParams(button_params);
//img_icon1.setOnClickListener(onMainImgClickHandler);
img_icon2.setLayoutParams(button_params);
img_icon3.setLayoutParams(button_params);
img_icon4.setLayoutParams(button_params);
希望這explaines我的問題。提前致謝。
問候 Nayeem
你想顯示整個屏幕上4個圖標? –