Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
223 views
in Technique[技术] by (71.8m points)

Place a view below another view at runtime when button is clicked Android Xamarin

I have a VideoView and a ListView in my RelativeLayout parent view. When the activity is Created by default the VideoView is below the ListView but I desire to change this and place the ListView below the VideoView when a button is clicked at runtime... I have tried this but am getting an error that :"Circular dependencies are not allowed in RelativeLayout".. Here is the code I have that is throwing an exception

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Circular dependencies are not allowed in RelativeLayout

From the shared error, you can not modify the inner existed control of the RelativeLayout. That means you only can add View from outside of the Root RelativeLayout.

For example , create another_layout.xml as follows, and it only contains a Control. You could modify it with your needs 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>

Now back to Root RelativeLayout, and add android:id="@+id/RootView" in its xml.

Then we can add another_layout.xml in Root RelativeLayout as follows:

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

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...