简体   繁体   English

如何启用不在视图中的 HorizontalScrollView 中的特定按钮

[英]How to enable a specific button in HorizontalScrollView which is not in view

I have stacked 7 buttons in a HorizontallScrollView.我在 HorizontallScrollView 中堆叠了 7 个按钮。 The first four buttons are always displayed by default and the remaining 3 buttons are not displayed.默认情况下始终显示前四个按钮,其余三个按钮不显示。 My question is how can I get the button number 7 to be displayed within the view when the app starts.我的问题是如何让应用程序启动时在视图中显示 7 号按钮。 I am remembering the position of the buttons in onPause() and resetting them in onResume().我记得 onPause() 中按钮的 position 并在 onResume() 中重置它们。 Consider the picture for a better understanding.考虑图片以获得更好的理解。 在此处输入图像描述

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

    }
}

The xml file for HorizontalScrollView is: 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.您可以使用 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>
    
    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM