android - 如何启用不在视图中的 HorizontalScrollView 中的特定按钮
问题描述
我在 HorizontallScrollView 中堆叠了 7 个按钮。默认情况下始终显示前四个按钮,其余三个按钮不显示。我的问题是如何让应用程序启动时在视图中显示 7 号按钮。我记得 onPause() 中按钮的位置并在 onResume() 中重置它们。考虑图片以获得更好的理解。
@Override
public void onPause() {
super.onPause();
SharedPreferences sharedPrefs = getActivity().getSharedPreferences("BUttons", MODE_PRIVATE);
SharedPreferences.Editor ed;
ed = sharedPrefs.edit();
ed.putInt("button", selectedButton);
ed.commit();
}
@Override
public void onResume() {
super.onResume();
SharedPreferences sharedPrefs = getActivity().getSharedPreferences("BUTTONS", MODE_PRIVATE);
int lastSelectedButton = sharedPrefs.getInt("button",0);
selectedButton = lastSelectedButton;
if(lastSelectedButton == 0){
buttonWatched.setEnabled(false);
buttonWatch.setEnabled(true);
buttonIgnore.setEnabled(true);
buttonPurchased.setEnabled(true);
buttonSaved.setEnabled(true);
buttonBuy.setEnabled(true);
buttonFavourites.setEnabled(true);
}
else if(lastSelectedButton == 1){
buttonWatched.setEnabled(true);
buttonWatch.setEnabled(false);
buttonIgnore.setEnabled(true);
buttonPurchased.setEnabled(true);
buttonSaved.setEnabled(true);
buttonBuy.setEnabled(true);
buttonFavourites.setEnabled(true);
}
else if(lastSelectedButton == 2){
buttonWatched.setEnabled(true);
buttonWatch.setEnabled(true);
buttonIgnore.setEnabled(false);
buttonPurchased.setEnabled(true);
buttonSaved.setEnabled(true);
buttonBuy.setEnabled(true);
buttonFavourites.setEnabled(true);
}
else if(lastSelectedButton == 3){
buttonWatched.setEnabled(true);
buttonWatch.setEnabled(true);
buttonIgnore.setEnabled(true);
buttonPurchased.setEnabled(false);
buttonSaved.setEnabled(true);
buttonBuy.setEnabled(true);
buttonFavourites.setEnabled(true);
}
else if(lastSelectedButton == 4){
buttonWatched.setEnabled(true);
buttonWatch.setEnabled(true);
buttonIgnore.setEnabled(true);
buttonPurchased.setEnabled(true);
buttonSaved.setEnabled(false);
buttonBuy.setEnabled(true);
buttonFavourites.setEnabled(true);
}
else if(lastSelectedButton == 5){
buttonWatched.setEnabled(true);
buttonWatch.setEnabled(true);
buttonIgnore.setEnabled(true);
buttonPurchased.setEnabled(true);
buttonSaved.setEnabled(true);
buttonBuy.setEnabled(false);
buttonFavourites.setEnabled(true);
}
else if(lastSelectedButton == 6){
buttonWatched.setEnabled(true);
buttonWatch.setEnabled(true);
buttonIgnore.setEnabled(true);
buttonPurchased.setEnabled(true);
buttonSaved.setEnabled(true);
buttonBuy.setEnabled(true);
buttonFavourites.setEnabled(false);
}
}
HorizontalScrollView 的 xml 文件是:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/colorWhite"
android:id="@+id/relative_hollywood">
<HorizontalScrollView
android:id="@+id/hsv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<LinearLayout
android:id="@+id/button_layout"
android:layout_width="match_parent"
android:layout_height="35dp">
<Button
android:id="@+id/buttonWatched"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button"
android:textColor="@drawable/color_selector"
android:text="Watched"/>
<Button
android:id="@+id/buttonWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button"
android:textColor="@drawable/color_selector"
android:text="Watch"/>
<Button
android:id="@+id/buttonIgnore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button"
android:textColor="@drawable/color_selector"
android:text="Ignore"/>
<Button
android:id="@+id/buttonPurchased"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button"
android:textColor="@drawable/color_selector"
android:text="Purchased"/>
<Button
android:id="@+id/buttonSaved"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button"
android:textColor="@drawable/color_selector"
android:text="Saved"/>
<Button
android:id="@+id/buttonBuy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button"
android:textColor="@drawable/color_selector"
android:text="Buy"/>
<Button
android:id="@+id/buttonFavourites"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button"
android:textColor="@drawable/color_selector"
android:text="Favourites"/>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
解决方案
You can do that with layout_weight property, and keeping android:layout_width=0dp for all buttons.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
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"
<LinearLayout
android:gravity="bottom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<Button
android:layout_weight="1"
android:text="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<Button
android:layout_weight="1"
android:text="2"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<Button
android:layout_weight="1"
android:text="3"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<Button
android:layout_weight="1"
android:text="4"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<Button
android:layout_weight="1"
android:text="5"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<Button
android:layout_weight="1"
android:text="6"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<Button
android:layout_weight="1"
android:text="7"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
</LinearLayout>
</RelativeLayout>
推荐阅读
- conv-neural-network - GRU 包含用于文本生成器的 CNN
- java - 将 Countrylist 模型、数据和 Arraylist 从 Java 重写为 kotlin
- javascript - 如何触发点击事件进入通过 Ajax 加载但未附加的 DOM
- asp.net-core - 未找到 WebApi Core 2.2 OData 资源/路径
- algorithm - 在快速排序中选择数据透视
- html - 在另一个页面上使用设计登录
- azure-active-directory - New-AzAks powershell cmdlet 无法指定与 AAD 集成所需的 server-app-id 和 server-app-secret
- javascript - postcss 正在处理 /css/app.css 并在 import vue2-daterange-picker 后抛出 CssSyntaxError
- macos - 在我添加标志之前可以访问 npm 包的二进制文件
- android - 如何使用 Jetpack Compose 创建动画?