问题描述
我在 HorizontallScrollView 中堆叠了 7 个按钮。默认情况下始终显示前四个按钮,不显示其余 3 个按钮。 我的问题是如何在应用程序启动时让按钮编号 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>
解决方法
您可以使用 layout_weight 属性来做到这一点,并为所有按钮保持 android:layout_width=0dp。
<?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>