如何在 Horizo​​ntalScrollView 中启用不在视图中的特定按钮

如何解决如何在 Horizo​​ntalScrollView 中启用不在视图中的特定按钮

我在 Horizo​​ntallScrollView 中堆叠了 7 个按钮。认情况下始终显示前四个按钮,不显示其余 3 个按钮。 我的问题是如何在应用程序启动时让按钮编号 7 显示在视图中。 我记得按钮在 onPause() 中的位置并在 onResume() 中重置它们。考虑一下图片以便更好地理解。

enter image description here

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

    }
}

Horizo​​ntalScrollView 的 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>
    
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?