微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

单击按钮时,在运行时将视图置于另一个视图下方 Android Xamarin

如何解决单击按钮时,在运行时将视图置于另一个视图下方 Android Xamarin

我的 RelativeLayout 父视图中有一个 VideoView 和一个 ListView。认情况下创建活动时,VideoView 位于 ListView 下方,但我希望更改此设置并将 ListView 在运行时单击按钮时放置在 VideoView 下方......我尝试过此操作,但出现以下错误:“循环依赖项不允许在相对布局中”.. 这是我抛出异常的代码

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如下,它只包含一个控件。您可以根据自己的需要进行修改。

<?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"

然后我们可以在RootRelativeLayout中添加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);

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。