如何从编程夸大的布局访问视图?

时间:2015-12-18 09:30:02

标签: java android layout views android-inflate

我有一项活动,其中我已经膨胀了另一种布局。点击下一个目的地我正在膨胀一个新的布局。现在我想删除膨胀的布局。所以我想访问从充气布局中删除的线性布局。我怎样才能访问这个?

布局如下所示:enter image description here

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

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>

布局膨胀的活动。

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);       
    }
});

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

1 个答案:

答案 0 :(得分:0)

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