Ash*_*hak 7 android gridview android-layout
我已经在xml布局中声明了GirdView,如下所示.
<GridView
android:id="@+id/movies_gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="true"
android:columnWidth="92dp"
android:numColumns="auto_fit"
android:stretchMode="spacingWidthUniform"
android:gravity="center_horizontal" />
Run Code Online (Sandbox Code Playgroud)
我打算在此处显示固定大小的图像(垂直矩形不是正方形),GridView.我将其设置为columnWidth,auto_fit以便可以根据GridView.可用的屏幕大小最佳地使用水平空间.我已设置为stretchMode,spacingWidthUniform以便将列放置在距离所有边相等的距离.
我想把verticalSpacing等于horizontalSpacing.我不能在xml中这样做,因为在运行时,horizontalSpacing它将取决于屏幕上的可用空间.此外,当设备方向改变时,horizontalSpacing将改变.
我的问题是:
我怎样才能确保verticalSpacing永远等于horizontalSpacing?
如何GridView从头开始布局其第一列加上最后一列的填充和结束布局到GridView减去填充的结尾?
编辑1
我尝试使用@simas解决方案来使用提到的自定义网格视图.为了那个原因,
我把这个类复制到了我的源目录.
在布局xml中包含此网格视图,如下所示.
<com.aviras.ashish.popularmoviesapp.widgets.CustomGridView
android:id="@+id/movies_gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="@dimen/grid_item_width"
android:drawSelectorOnTop="true"
android:gravity="center_horizontal"
android:numColumns="auto_fit"
android:stretchMode="spacingWidthUniform"
tools:listitem="@layout/grid_item_movie_poster" />
Run Code Online (Sandbox Code Playgroud)
将适配器设置为此gridview,如下所示
CustomGridView moviesGridView = (CustomGridView) rootView.findViewById(R.id.movies_gridview);
moviesGridView.setAdapter(new MoviesAdapter(rootView.getContext().getApplicationContext(), mMovies));
Run Code Online (Sandbox Code Playgroud)它仍然显示列和行的不同间距,如下所示.

编辑2
@simas在第二个问题中通过'开始布局'我的意思是GridView应该开始绘制第一列在应该结束时的GridView最后一列和最后一列.此外,列和行应具有相等的间距.如果这更能澄清我的问题,请告诉我.GridViewGridView
setHorizontalSpacing您可以创建自定义 GridView 类并修改和的功能,setVerticalSpacing如下所示:
public class CustomGridView extends GridView {
public CustomGridView(Context context) {
super(context);
}
public CustomGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomGridView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public CustomGridView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public void setHorizontalSpacing(int horizontalSpacing) {
super.setHorizontalSpacing(horizontalSpacing);
// Call parent class to set the vertical spacing to be equal to the horizontal spacing
super.setVerticalSpacing(horizontalSpacing);
}
@Override
public void setVerticalSpacing(int verticalSpacing) {
// Prevent the derived class from modifying the vertical spacing
/*super.setVerticalSpacing(verticalSpacing);*/
}
}
Run Code Online (Sandbox Code Playgroud)
然后在 xml 中使用自定义类,如下所示:
至于你的第二个问题:我不太明白你所说的“开始布局”是什么意思。
您想要的结果的图像示例会有很大帮助。
| 归档时间: |
|
| 查看次数: |
565 次 |
| 最近记录: |