我有两个水平滚动视图。我如何一次显示一个并在单击按钮时在它们之间切换?
I have two horizontal scroll views. How can I show one at a time and switch between them on button click?
在我的 android 应用程序中,我有两个用作停靠栏的水平滚动视图。我怎样才能做到这一点,如果单击按钮 1,则显示停靠栏 1,如果单击按钮 2,则显示停靠栏 2?因为它们是码头,所以我希望它们都显示在同一位置。
这是我的代码:
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button5" />
</LinearLayout>
</HorizontalScrollView>
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button5" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dockButton1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dockButton2" />
按钮代码:
dock1button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
dock2button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
第一步:
在两个 HorizontalScrollView 中添加 id
例如:android:id="@+id/hScrollView1"
和 android:id="@+id/hScrollView2"
第 2 步:
将两个 HorizontalScrollView 都放在父 LinearLayout 中,如下所示:
<LinearLayout>
<HorizontalScrollView
android:id="@+id/hScrollView1">
.
.
</HorizontalScrollView>
<HorizontalScrollView
android:id="@+id/hScrollView2">
.
.
</HorizontalScrollView>
</LinearLayout>
第 3 步:
在您的按钮中单击只需切换可见性:
在 onClick 中只需使用..
hScrlView1.setVisibility(View.GONE);
hScrlView2.setVisibility(View.VISIBLE);
和
hScrlView1.setVisibility(View.VISIBLE);
hScrlView2.setVisibility(View.GONE);
其中 hScrlView1 和 hScrlView2 是您要显示的 ScrollView。
将两个水平的listview放在一个相对布局中,并将其中一个的可见性设置为不可见。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button5" />
</LinearLayout>
</HorizontalScrollView>
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:visibility="invisible" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button5" />
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dockButton1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dockButton2" />
然后更改按钮代码中的可见性:
按钮代码:
dock1button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
horizontalList2.setVisibilty(View.INVISIBLE);
horizontalList1.setVisibilty(View.VISIBLE);
}
});
dock2button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
horizontalList1.setVisibilty(View.INVISIBLE);
horizontalList2.setVisibilty(View.VISIBLE);
}
});
最好使用包含两个滚动视图的 FrameLayout
。它比使用 LinearLayout
甚至更糟 RelativeLayout
.
性能更高
就这样吧(原理图代码,省略了一些xml属性)
<FrameLayout>
<HorizontalScrollView
android:id="@+id/view1"/>
<HorizontalScrollView
android:id="@+id/view2"
android:visibility="gone"
/>
</FrameLayout>
然后在您的 onClick 处理程序中执行类似
的操作
view1.setVisibility(View.VISIBLE)
view2.setVisibility(View.GONE)
反之亦然(取决于按下的按钮)
在我的 android 应用程序中,我有两个用作停靠栏的水平滚动视图。我怎样才能做到这一点,如果单击按钮 1,则显示停靠栏 1,如果单击按钮 2,则显示停靠栏 2?因为它们是码头,所以我希望它们都显示在同一位置。
这是我的代码:
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button5" />
</LinearLayout>
</HorizontalScrollView>
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button5" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dockButton1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dockButton2" />
按钮代码:
dock1button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
dock2button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
第一步:
在两个 HorizontalScrollView 中添加 id
例如:android:id="@+id/hScrollView1"
和 android:id="@+id/hScrollView2"
第 2 步: 将两个 HorizontalScrollView 都放在父 LinearLayout 中,如下所示:
<LinearLayout>
<HorizontalScrollView
android:id="@+id/hScrollView1">
.
.
</HorizontalScrollView>
<HorizontalScrollView
android:id="@+id/hScrollView2">
.
.
</HorizontalScrollView>
</LinearLayout>
第 3 步: 在您的按钮中单击只需切换可见性: 在 onClick 中只需使用..
hScrlView1.setVisibility(View.GONE);
hScrlView2.setVisibility(View.VISIBLE);
和
hScrlView1.setVisibility(View.VISIBLE);
hScrlView2.setVisibility(View.GONE);
其中 hScrlView1 和 hScrlView2 是您要显示的 ScrollView。
将两个水平的listview放在一个相对布局中,并将其中一个的可见性设置为不可见。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button5" />
</LinearLayout>
</HorizontalScrollView>
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:visibility="invisible" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button5" />
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dockButton1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dockButton2" />
然后更改按钮代码中的可见性:
按钮代码:
dock1button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
horizontalList2.setVisibilty(View.INVISIBLE);
horizontalList1.setVisibilty(View.VISIBLE);
}
});
dock2button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
horizontalList1.setVisibilty(View.INVISIBLE);
horizontalList2.setVisibilty(View.VISIBLE);
}
});
最好使用包含两个滚动视图的 FrameLayout
。它比使用 LinearLayout
甚至更糟 RelativeLayout
.
就这样吧(原理图代码,省略了一些xml属性)
<FrameLayout>
<HorizontalScrollView
android:id="@+id/view1"/>
<HorizontalScrollView
android:id="@+id/view2"
android:visibility="gone"
/>
</FrameLayout>
然后在您的 onClick 处理程序中执行类似
的操作view1.setVisibility(View.VISIBLE)
view2.setVisibility(View.GONE)
反之亦然(取决于按下的按钮)