【问题标题】:How to align items at the top of a GridView in Android?如何在 Android 中对齐 GridView 顶部的项目?
【发布时间】:2014-05-26 12:50:05
【问题描述】:

我尝试制作自己的画廊应用程序。问题是我的图像视图出现在包含它们的网格视图的底部。如何将它们对齐在顶部?我尝试设置重力,但它不起作用。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gallery"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/crud_layout" >

    <GridView
        android:id="@+id/album_grid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:columnWidth="100dp"
        android:gravity="center"
        android:horizontalSpacing="2dp"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:verticalSpacing="2dp" />

</FrameLayout>

<LinearLayout
    android:id="@id/crud_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" >

    <Button
        android:id="@+id/button_share"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="@string/new_album" />

    <Button
        android:id="@+id/button_delete"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="@string/delete_album" />

    <ImageButton
        android:id="@+id/button_camera"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" >
    </ImageButton>
</LinearLayout>

【问题讨论】:

    标签: android android-layout gridview gravity layout-gravity


    【解决方案1】:

    编辑您的代码,如下所示:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gallery"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        **android:layout_alignParentTop="true"**//change only here
        android:layout_above="@+id/crud_layout" >
    
        <GridView
            android:id="@+id/album_grid"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:columnWidth="100dp"
            android:gravity="center"
            android:horizontalSpacing="2dp"
            android:numColumns="auto_fit"
            android:stretchMode="columnWidth"
            android:verticalSpacing="2dp" />
    
    </FrameLayout>
    
    <LinearLayout
        android:id="@id/crud_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >
    
        <Button
            android:id="@+id/button_share"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="zee" />
    
        <Button
            android:id="@+id/button_delete"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="khan" />
    
        <ImageButton
            android:id="@+id/button_camera"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" >
        </ImageButton>
    </LinearLayout>
    </RelativeLayout>
    

    【讨论】:

      猜你喜欢
      • 2016-03-01
      • 1970-01-01
      • 2021-03-29
      • 2021-08-30
      • 2017-09-10
      • 2020-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多