我是 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);
希望这能解释我的问题。提前致谢。
问候纳耶姆