java - 如何从以编程方式膨胀的布局中访问 View ?

标签 java android layout view android-inflate

我有一个 Activity ,其中我夸大了另一个布局。单击下一个目的地时,我会得到一个新的布局。现在我想删除膨胀的布局。所以我想访问从内部膨胀布局中删除的线性布局。我怎样才能访问这个?

布局如下所示:enter image description here

我已经实现了膨胀布局并从父级中删除第一个目的地,这是父级布局的一部分。现在我想对膨胀的目标布局做同样的事情。删除膨胀布局的 onClick 应该被删除。

Next_destination 布局

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

<LinearLayout   xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="wrap_content"
    android:layout_weight="1.00"
    android:background="@android:color/white"
    android:id="@+id/LinearAddedDestination">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="DESTINATION"
            android:layout_marginStart="20dp"
            android:layout_marginTop="20dp" />
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:orientation="horizontal"
            android:background="@drawable/shape"
            android:gravity="right"
            android:layout_gravity="center|right"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="80dp"
            android:id="@+id/remove">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_clear_black_18dp"
                android:layout_marginLeft="05dp"
                android:layout_marginTop="05dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="REMOVE"
                android:layout_gravity="center"
                android:layout_marginLeft="05dp"
                android:textSize="14sp"
                android:layout_marginRight="15dp" />
        </LinearLayout>
    </LinearLayout>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="08dp"
        android:background="@drawable/line2"/>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_marginStart="20dp"
            android:background="@drawable/ic_place_black_48dp"
            android:layout_marginTop="05dp"
            android:layout_gravity="center" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:drawableEnd="@drawable/ic_chevron_right_black_24dp"
            android:layout_marginEnd="10dp"
            android:text="@string/Loc"
            android:layout_gravity="center"
            android:layout_marginTop="05dp"
            android:textAppearance="@android:style/TextAppearance.Medium"
            android:cursorVisible="false"/>

    </LinearLayout>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Street/Building/Place"
        android:layout_marginStart="48dp"/>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_marginStart="20dp"
            android:background="@drawable/ic_description_black_48dp"
            android:layout_marginTop="10dp" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_marginEnd="10dp"
            android:hint="@string/LocationDetails"/>

    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:layout_marginStart="48dp"
        android:hint="@string/HouseDetails" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal">

        <ImageView
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_marginStart="20dp"
            android:background="@drawable/ic_person_black_48dp"
            android:layout_marginTop="05dp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="10dp"
            android:text="@string/additionalContact"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:drawableEnd="@drawable/ic_expand_more_black_24dp"
            android:layout_marginTop="08dp"
            android:layout_marginStart="05dp" />
    </LinearLayout>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="60dp"
        android:layout_marginEnd="50dp"
        android:layout_gravity="center"
        android:visibility="gone"
        android:layout_marginTop="20dp">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="Name"
            android:layout_gravity="center"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="Phone"
            android:layout_gravity="center"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="@string/or"
            android:layout_gravity="center"
            android:layout_marginTop="20dp" />

        <LinearLayout
            android:layout_width="220dp"
            android:layout_height="40dp"
            android:background="@drawable/shape"
            android:layout_gravity="center_horizontal">


            <ImageButton
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:background="@drawable/phone2"
                android:layout_gravity="center"
                android:layout_marginStart="30dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/addFromContacts"
                android:layout_gravity="center"
                android:layout_marginStart="20dp" />
        </LinearLayout>

    </LinearLayout>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:layout_marginLeft="48dp"
        android:hint="@string/contact1"
        android:layout_marginTop="10dp" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:focusable="true"
        android:focusableInTouchMode="true">

        <ImageView
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_marginStart="20dp"
            android:background="@drawable/ic_description_black_48dp"
            android:layout_marginTop="10dp" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_marginEnd="10dp"
            android:hint="Instruction"
            android:layout_gravity="center"
            android:layout_marginLeft="10dp" />

    </LinearLayout>

</LinearLayout>

布局膨胀的 Activity 。

View newDestination;
ViewGroup parent;


nextDestination.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        removeDestination.setVisibility(View.VISIBLE);
        newDestination = getLayoutInflater().inflate(
            R.layout.next_destination_layout, addDestination, true);
    }
}); 


removeDestination.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        parent = (ViewGroup)destination.getParent();
        parent.removeView(destination);       
    }
});

如何实现这一目标?请帮忙...

最佳答案

{ ll.removeView(view)// to remove particular view
  ll.removeViewAt(position);// to remove view from particular position
}

关于java - 如何从以编程方式膨胀的布局中访问 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34352340/

相关文章:

java - Android 应用程序从长时间空闲状态恢复时崩溃

java - 使用 Bukkit/类路径

android - 显示嵌套的 LinearLayout 时出错

android - Appium (v1.4.16.1),第二次尝试切换到 WebView 失败

android - 改变线性布局android中的 View 顺序

java - 如何使用 REST API 发布 Yammer 民意调查?

java - 创建多个全局常数的正确方法?

android - 从 Android NDK 在 gcc 中交叉编译 gmp,无法执行 ./gen-fac_ui

带有 ScrollView 的android自定义对话框将按钮推离屏幕

html - 我怎样才能模仿表格的流体单元格宽度但仍然允许换行?