【发布时间】:2020-08-02 15:05:41
【问题描述】:
项目将显示在GridView:
<GridView
android:id="@+id/month_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/activity_half_horizontal_margin"
android:paddingRight="@dimen/activity_half_horizontal_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:stretchMode="columnWidth"
android:horizontalSpacing="4dp"
android:verticalSpacing="4dp"/>
用于在代码中生成的项目的布局:
private LinearLayout getMonthImageView() {
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
TextView text = new TextView(context);
text.setGravity(Gravity.CENTER);
text.setSingleLine();
layout.addView(text, 0);
ImageView image = new ImageView(context);
image.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
image.setAdjustViewBounds(true);
layout.addView(image, 1);
return layout;
}
我已经将该部分重构为来自 xml 布局文件:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/month_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:singleLine="true"/>
<ImageView
android:id="@+id/month_icon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerInside"
android:adjustViewBounds="true"/>
</LinearLayout>
这产生了不想要的可见效果:GridView 中的项目之间的差距变大了,就好像这些项目获得了一些填充或边距(显然不是这种情况)。这是为什么?在代码中生成的项目与来自 xml 布局文件的项目有什么区别?如何让物品再次放大?
【问题讨论】:
标签: android android-layout android-xml android-gridview