Android 如何在单击按钮时切换listview的位置
我有一个列表视图,最初放置在按钮上方,但单击按钮后,我希望按钮放置在列表视图的顶部。下面是xml。任何帮助都将不胜感激Android 如何在单击按钮时切换listview的位置,android,android-layout,listview,android-relativelayout,Android,Android Layout,Listview,Android Relativelayout,我有一个列表视图,最初放置在按钮上方,但单击按钮后,我希望按钮放置在列表视图的顶部。下面是xml。任何帮助都将不胜感激 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:id="@+id/parentView"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/parentView"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ListView
android:id="@+id/likes_list"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:padding="20dp" />
<ListView
android:id="@+id/comments_list"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:padding="20dp" />
<Button
android:id="@+id/like_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/likes_list"
android:text="Likes"
android:onClick="onClickLikeButton" />
<Button
android:id="@+id/comment_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/likes_list"
android:layout_toRightOf="@+id/like_button"
android:text="Comments"
android:onClick="onClickCommentButton" />
在代码中,使用findViewById和get按钮。然后,设置LayoutParams,如下所示:
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) button2.getLayoutParams();
rlp.addRule(RelativeLayout.ABOVE, R.id.listViewId);
button2.setLayoutParams(rlp);
您可以初始设置两个按钮,一个在列表上方,一个在列表下方。并将可见性设置为高于一个,但单击“列表项”“设置可见性”“视图”并将其设置为低于一个按钮。