动态添加视图并设置参数和规则无法正常工作 (android)
Adding View dynamically and setting Parameters and rules is not working properly (android)
我的 android 项目遇到了问题。这里我有一个 imageButton ,点击它我必须膨胀布局文件并将其动态添加到 Relative layout 中。添加视图效果很好。但问题是在添加此子视图后我需要将图像按钮移动到新的子布局下方。但是我的 imageButton 在子视图下膨胀,也许我缺少一些 LayoutParameters 和规则。
这是我的代码
private void addDestinationViews() {
View destinationChildView = LayoutInflater.from(getActivity()).inflate(R.layout.destination_city_item, null);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params.addRule(RelativeLayout.ALIGN_TOP, R.id.add_destinationbutton);
destinationChildView.setLayoutParams(params);
mInflated_view_holder.addView(destinationChildView);
}
这是我要膨胀的布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/add_destination_button_back"
android:text="afaf"
android:textSize="18sp"
android:paddingLeft="30dp"
android:padding="10dp"
android:gravity="start|center"/>
<ImageButton
android:id="@+id/cancle_destination_imagebutton"
android:layout_width="10dp"
android:layout_height="10dp"
android:background="@drawable/cross_place_icon"
android:scaleType="centerInside"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"/>
这是将承载膨胀布局的相对布局
<RelativeLayout
android:id="@+id/inflated_view_holder_three"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="@+id/child_counter_title"
android:layout_marginTop="15dp">
<ImageButton
android:id="@+id/add_destinationbutton"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/add_place_icon"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:background="@drawable/add_destination_button_back"/>
</RelativeLayout>
P.S 我忘了说这个子视图将在按下 imageButton 时添加。所以这里的代码需要是动态的。
使用它来托管您的膨胀布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/inflated_view_holder_three"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="15dp">
</RelativeLayout>
<ImageButton
android:id="@+id/add_destinationbutton"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_below="@+id/inflated_view_holder_three"
android:layout_marginRight="5dp"
android:src="@drawable/ic_launcher" />
<!--android:background="@drawable/add_destination_button_back"-->
</RelativeLayout>
如有任何问题请告诉我
我的 android 项目遇到了问题。这里我有一个 imageButton ,点击它我必须膨胀布局文件并将其动态添加到 Relative layout 中。添加视图效果很好。但问题是在添加此子视图后我需要将图像按钮移动到新的子布局下方。但是我的 imageButton 在子视图下膨胀,也许我缺少一些 LayoutParameters 和规则。
这是我的代码
private void addDestinationViews() {
View destinationChildView = LayoutInflater.from(getActivity()).inflate(R.layout.destination_city_item, null);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params.addRule(RelativeLayout.ALIGN_TOP, R.id.add_destinationbutton);
destinationChildView.setLayoutParams(params);
mInflated_view_holder.addView(destinationChildView);
}
这是我要膨胀的布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/add_destination_button_back"
android:text="afaf"
android:textSize="18sp"
android:paddingLeft="30dp"
android:padding="10dp"
android:gravity="start|center"/>
<ImageButton
android:id="@+id/cancle_destination_imagebutton"
android:layout_width="10dp"
android:layout_height="10dp"
android:background="@drawable/cross_place_icon"
android:scaleType="centerInside"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"/>
这是将承载膨胀布局的相对布局
<RelativeLayout
android:id="@+id/inflated_view_holder_three"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="@+id/child_counter_title"
android:layout_marginTop="15dp">
<ImageButton
android:id="@+id/add_destinationbutton"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/add_place_icon"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:background="@drawable/add_destination_button_back"/>
</RelativeLayout>
P.S 我忘了说这个子视图将在按下 imageButton 时添加。所以这里的代码需要是动态的。
使用它来托管您的膨胀布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/inflated_view_holder_three"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="15dp">
</RelativeLayout>
<ImageButton
android:id="@+id/add_destinationbutton"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_below="@+id/inflated_view_holder_three"
android:layout_marginRight="5dp"
android:src="@drawable/ic_launcher" />
<!--android:background="@drawable/add_destination_button_back"-->
</RelativeLayout>
如有任何问题请告诉我