Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android-GridView_Android_Xml_Gridview - Fatal编程技术网

Android-GridView

Android-GridView,android,xml,gridview,Android,Xml,Gridview,下面是我的下一个问题 我想将我的GridView居中,我在网上搜索了几个小时,我发现其中一个解决方案是将我的GridView放在相对布局中,然后使用android:Layout\u centerVertical=“true”属性将其居中,但它不起作用:( 你能看一下代码并给我一些反馈吗?(这个问题是关于第二个GridView的,id:tutorTypeGridView) 您是否尝试使用android:layout\u centerInParent=“true” 它需要在RelativeLayou

下面是我的下一个问题

我想将我的GridView居中,我在网上搜索了几个小时,我发现其中一个解决方案是将我的GridView放在相对布局中,然后使用android:Layout\u centerVertical=“true”属性将其居中,但它不起作用:(

你能看一下代码并给我一些反馈吗?(这个问题是关于第二个GridView的,id:tutorTypeGridView)


您是否尝试使用android:layout\u centerInParent=“true”
它需要在RelativeLayout中

我通过创建一个新函数来解决它,该函数计算中心GridView所需的填充,这里是:(请记住,我在片段中使用它)


是的,获取“线性布局中的布局参数无效”错误编辑:也许我不应该使用约束布局?编辑2:不,不是这样。不要在线性布局中使用RelativeLayout,请稍后使用
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/tutorSignupLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.SignupStepOneActivity" >

<TextView
    android:id="@+id/additionalInfoTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="16dp"
    android:text="@string/signup_step_one_under_welcome"
    android:textSize="16sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:orientation="vertical"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/additionalInfoTextView" >

    <TextView
        android:id="@+id/chooseProfessionTextView"
        android:layout_width="match_parent"
        android:layout_height="39dp"
        android:gravity="center"
        android:text="Choose your profession" />

    <GridView
        android:id="@+id/professionGridView"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/profession_buttons"
        android:columnWidth="90dp"
        android:gravity="center"
        android:horizontalSpacing="6dp"
        android:numColumns="auto_fit"
        android:verticalSpacing="6dp" >

    </GridView>

    <TextView
        android:id="@+id/chooseTutorType"
        android:layout_width="match_parent"
        android:layout_height="39dp"
        android:gravity="center"
        android:text="Choose your profession" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <GridView
        android:id="@+id/tutorTypeGridView"
        android:layout_width="match_parent"
        android:layout_height="@dimen/tutor_types_buttons"
        android:columnWidth="90dp"
        android:horizontalSpacing="6dp"
        android:numColumns="auto_fit"
        android:verticalSpacing="6dp"
        android:layout_centerVertical="true"
        android:stretchMode="columnWidth" >

    </GridView>

    </RelativeLayout>


</LinearLayout>

</android.support.constraint.ConstraintLayout>
    public void centerGridView(GridView gridView) {

    DisplayMetrics displayMetrics = new DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

    int width = displayMetrics.widthPixels;

    int girdWidth = gridView.getWidth();

    int padding = width - girdWidth;

    padding = convertPxToDp(padding);

    gridView.setPadding(padding / 2, 0, padding / 2, 0);

}

    public int convertPxToDp(int px){
    return Math.round(px/(getResources().getSystem().getDisplayMetrics().xdpi/DisplayMetrics.DENSITY_DEFAULT));
}