Android 是否以编程方式从GridLayout中删除行?

Android 是否以编程方式从GridLayout中删除行?,android,android-layout,Android,Android Layout,是否可以通过编程方式从GridLayout中删除行? 下面的布局由3行组成(电话、手机、电子邮件)。 在代码中,它可以确定是否没有手机号码。如果不是,那么我根本不想显示第二行 <?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:mapbox="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/to

是否可以通过编程方式从GridLayout中删除行?
下面的布局由3行组成(电话、手机、电子邮件)。 在代码中,它可以确定是否没有手机号码。如果不是,那么我根本不想显示第二行

<?xml version="1.0" encoding="utf-8"?>

<GridLayout xmlns:mapbox="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/individual_gridlayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:alignmentMode="alignBounds"
    android:columnCount="4"
    android:columnOrderPreserved="false"
    android:rowCount="3">


    <ImageButton
        android:id="@+id/phone_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="1"
        android:layout_row="0"
        android:background="@null"
        android:src="@drawable/ic_phone_enabled" />

    <ImageButton
        android:id="@+id/voip_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="2"
        android:layout_row="0"
        android:background="@null"
        android:src="@drawable/ic_voip_enabled" />

    <TextView
        android:id="@+id/phone_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="3"
        android:layout_margin="10dp"
        android:layout_row="0"
        android:text="My Phone number"
        android:textColor="@color/black"
        android:textSize="@dimen/text_size_small"
        tools:text="phone" />


    <ImageButton
        android:id="@+id/message_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="0"
        android:layout_row="1"
        android:background="@null"
        android:src="@drawable/ic_textsms_enabled" />

    <ImageButton
        android:id="@+id/cell_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="1"
        android:layout_row="1"
        android:background="@null"
        android:src="@drawable/ic_phone_enabled" />

    <ImageButton
        android:id="@+id/voip2_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="2"
        android:layout_row="1"
        android:background="@null"
        android:src="@drawable/ic_voip_enabled" />

    <TextView
        android:id="@+id/cell_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="3"
        android:layout_margin="10dp"
        android:layout_row="1"
        android:text="My Cell number"
        android:textColor="@color/black"
        android:textSize="@dimen/text_size_small"
        tools:text="phone" />

    <ImageButton
        android:id="@+id/email_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="0"
        android:layout_row="2"
        android:background="@null"
        android:src="@drawable/ic_email_enabled" />

    <TextView
        android:id="@+id/email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="1"
        android:layout_columnSpan="3"
        android:layout_margin="10dp"
        android:layout_row="2"
        android:text="someone@state.nm.us"
        android:textColor="@color/black"
        android:textSize="@dimen/text_size_small"
        tools:text="email" />

</GridLayout>

有很多方法可以解决这个问题。我最后要做的是创建另一个布局文件,而不包含有问题的行,然后根据某些条件选择适当的布局。然后在代码中,您还必须使用此条件围绕可能不存在的组件跳舞

if (some condition) {
    view = inflater.inflate(R.layout.mylayout, container, false);
}
else {
    view = inflater.inflate(R.layout.mylayout_no_cell, container, false);
}
mylayout\u no\u cell.xml

<GridLayout xmlns:mapbox="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/individual_gridlayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:alignmentMode="alignBounds"
    android:columnCount="4"
    android:columnOrderPreserved="false"
    android:rowCount="2">


    <ImageButton
        android:id="@+id/phone_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="1"
        android:layout_row="0"
        android:background="@null"
        android:src="@drawable/ic_phone_enabled" />

    <ImageButton
        android:id="@+id/voip_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="2"
        android:layout_row="0"
        android:background="@null"
        android:src="@drawable/ic_voip_enabled" />

    <TextView
        android:id="@+id/phone_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="3"
        android:layout_margin="10dp"
        android:layout_row="0"
        android:text="My Phone number"
        android:textColor="@color/black"
        android:textSize="@dimen/text_size_small"
        tools:text="phone" />

    <ImageButton
        android:id="@+id/email_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="0"
        android:layout_row="1"
        android:background="@null"
        android:src="@drawable/ic_email_enabled" />

    <TextView
        android:id="@+id/email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="1"
        android:layout_columnSpan="3"
        android:layout_margin="10dp"
        android:layout_row="1"
        android:text="someone@state.nm.us"
        android:textColor="@color/black"
        android:textSize="@dimen/text_size_small"
        tools:text="email" />

</GridLayout>

有很多方法可以解决这个问题。我最后要做的是创建另一个布局文件,而不包含有问题的行,然后根据某些条件选择适当的布局。然后在代码中,您还必须使用此条件围绕可能不存在的组件跳舞

if (some condition) {
    view = inflater.inflate(R.layout.mylayout, container, false);
}
else {
    view = inflater.inflate(R.layout.mylayout_no_cell, container, false);
}
mylayout\u no\u cell.xml

<GridLayout xmlns:mapbox="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/individual_gridlayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:alignmentMode="alignBounds"
    android:columnCount="4"
    android:columnOrderPreserved="false"
    android:rowCount="2">


    <ImageButton
        android:id="@+id/phone_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="1"
        android:layout_row="0"
        android:background="@null"
        android:src="@drawable/ic_phone_enabled" />

    <ImageButton
        android:id="@+id/voip_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="2"
        android:layout_row="0"
        android:background="@null"
        android:src="@drawable/ic_voip_enabled" />

    <TextView
        android:id="@+id/phone_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="3"
        android:layout_margin="10dp"
        android:layout_row="0"
        android:text="My Phone number"
        android:textColor="@color/black"
        android:textSize="@dimen/text_size_small"
        tools:text="phone" />

    <ImageButton
        android:id="@+id/email_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="0"
        android:layout_row="1"
        android:background="@null"
        android:src="@drawable/ic_email_enabled" />

    <TextView
        android:id="@+id/email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="1"
        android:layout_columnSpan="3"
        android:layout_margin="10dp"
        android:layout_row="1"
        android:text="someone@state.nm.us"
        android:textColor="@color/black"
        android:textSize="@dimen/text_size_small"
        tools:text="email" />

</GridLayout>

网格布局适用于单元格,而不是行,它允许您更改列的数量。 更适合使用以下方法移除不需要的电池:

GridLayout grid = findViewById(R.id.grid);
Button btnInGrid = findViewById(R.id.someButtonInsideTheGrid);
grid.removeView(btnInGrid);

网格布局适用于单元格,而不是行,它使您能够更改列的数量。 更适合使用以下方法移除不需要的电池:

GridLayout grid = findViewById(R.id.grid);
Button btnInGrid = findViewById(R.id.someButtonInsideTheGrid);
grid.removeView(btnInGrid);

删除第三个和第四个孩子,或者选择更合适的布局/widget您认为更合适的布局是什么?可能是TableLayout删除第三个和第四个孩子,或者选择更合适的布局/widget您认为更合适的布局是什么?可能是TableLayout这个答案被否决了,我认为这不是一个最佳解决方案。我之所以这样做是因为1。不显示行的能力要求在事实和2之后。时间至关重要。这个答案被否决了,我知道这不是一个最优的解决方案。我之所以这样做是因为1。不显示行的能力要求在事实和2之后。时间至关重要。