[英]Place a view below another view at runtime when button is clicked Android Xamarin
我的 RelativeLayout 父视图中有一个 VideoView 和一个 ListView。 默认情况下创建活动时,VideoView 位于 ListView 下方,但我希望在运行时单击按钮时更改此设置并将 ListView 放置在 VideoView 下方...我尝试过此操作,但收到错误消息:“循环依赖项在RelativeLayout中是不允许的”..这是我抛出异常的代码
class Audio_Player:AppCompatActivity{
ListView audiolist;
VideoView video;
Button change;
protected override void OnCreate(Bundle savedInstanceState){
audiolist=(ListView)FindViewById<ListView>(Resource.Id.myaudiolist;
video=(VideoView)FindViewById<VideoView>(Resource.Id.myvideo);
change=(Button)FindViewById<Button>(Resource.Id.button1);
change.Click += layout_change;
}
//method to change the layout rule on button click
private void layout_change(object sender,EventArgs e) {
RelativeLayout.LayoutParams layoutParams=new RelativeLayout.LayoutParams(WindowManagerLayoutParams.MatchParent, WindowManagerLayoutParams.WrapContent);
LayoutParams.AddRule(LayoutRules.Below,video.Id);
audiolist.LayoutParameters=layoutParams;
}
}
RelativeLayout 中不允许循环依赖
从共享错误中,您无法修改RelativeLayout的内部现有控件。 这意味着您只能从 Root RelativeLayout之外添加 View 。
比如创建another_layout.xml如下,它只包含一个Control。 您可以通过您的需求控制对其进行修改。
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout 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/AnotherRootView"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:text="AnotherListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minWidth="25px"
android:minHeight="25px"
android:id="@+id/AnotherListView" />
</LinearLayout>
现在回到 Root RelativeLayout,并在其xml中添加android:id="@+id/RootView"
。
然后我们可以在 Root RelativeLayout 中添加another_layout.xml
如下:
private void Change_Click(object sender, System.EventArgs e)
{
// get root view
RelativeLayout rootView = (RelativeLayout)FindViewById<RelativeLayout>(Resource.Id.RootView);
// set layout params
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(WindowManagerLayoutParams.MatchParent, WindowManagerLayoutParams.WrapContent);
layoutParams.AddRule(LayoutRules.Below, video.Id);
// get another layout
LayoutInflater inflater = (LayoutInflater)this.GetSystemService(Context.LayoutInflaterService);
LinearLayout linearLayout = (LinearLayout)inflater.Inflate(Resource.Layout.another_layout, null,true);
// get and set data for anther list view
ListView anotherListView = (ListView)linearLayout.FindViewById(Resource.Id.AnotherListView);
// we can remove the previous list view
rootView.RemoveView(audiolist);
// add another list view which inside the anther layout
rootView.AddView(linearLayout, layoutParams);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.