【问题标题】:Add buttons dynamically to ScrollView android将按钮动态添加到 ScrollView android
【发布时间】:2018-03-04 20:13:11
【问题描述】:

所以我想在 android 的布局中添加 x 数量的按钮(其中 x 通常在 4 到 24 之间),我可以使用下面的代码实现这一点,但它不会滚动,所以它会切断一些按钮

我正在使用一个片段,其中包含 LinearLayoutBottomNavigation,其中一个导航选项会导致下面包含的片段

目前 my.xml 文件如下所示:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/units_group"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_marginEnd="20dp"
        android:layout_marginStart="20dp"
        android:layout_marginTop="20dp"
        android:orientation="vertical">

    </LinearLayout>

</ScrollView>

还有我的代码:

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_course_units, container, false);

    final LinearLayout unitsBtns = (LinearLayout) view.findViewById(R.id.units_group);

    //I actually get this from a volley request but didn't want to include all the code
    String[] units = {"1", "2", "3", "4", "5", "6", "7", "8"}; 

    for(int i = 0; i < units.length; i++){
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
        );

        params.setMargins(0, 30, 0, 0);
        Button b = new Button(getActivity());

        b.setLayoutParams(params);
        b.setText(units[i]);
        b.setId(i);
        b.setBackgroundColor(Color.WHITE);

        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    // STUFF HERE
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        unitsBtns.addView(b);
    }

    return view;
}

有没有办法让按钮可以滚动?我也尝试将 scrollView 放在它的父级周围,但它似乎仍然不起作用。

注意:我对开发原生 Android 应用程序还是很陌生,所以如果我做错了什么,请纠正我

【问题讨论】:

  • 使用NestedScrollView 而不是ScrollView
  • 看我的帖子对你有帮助..
  • 我不知道这是否会有所帮助,但您可以使用包含按钮的 listView 或 recyclerview。两者都是可滚动的。

标签: java android xml android-fragments


【解决方案1】:

当你必须使用 ScrollView 时,你应该在 ScrollView 的子视图中提供 android:layout_height="wrap_content" 然后你只需要滚动你的视图

你应该像这样使用用户LinearLayout

<LinearLayout
        android:id="@+id/units_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginEnd="20dp"
        android:layout_marginStart="20dp"
        android:layout_marginTop="20dp"
        android:orientation="vertical">

    </LinearLayout>

这里也需要替换

  LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);

注意:当你在高度属性中给出match_parent时,它会将你的视图限制在设备屏幕上

希望对你有所帮助.. :)

【讨论】:

  • 谢谢!原来这是混合的,我忘记了从 BottomNavigationView 的偏移量,所以它隐藏了一些底部按钮
【解决方案2】:

这是一个工作示例(水平):

<HorizontalScrollView
        android:id="@+id/hsv1"
        android:scrollbars="none"
        android:background="#0d47a1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:id="@+id/hsvLayout1"
            android:background="#0d47a1"
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </LinearLayout>
</HorizontalScrollView>

获取 ScrollView 和布局:

HorizontalScrollView hsv1 = (HorizontalScrollView) findViewById( R.id.hsv1 );
LinearLayout layout = (LinearLayout) hsv1.findViewById( R.id.hsvLayout1 );
layout.removeAllViews();

动态创建按钮:

Button myButton = new Button (this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT );
lp.setMargins( 20, 0, 20, 0 );
myButton.setLayoutParams(lp);
myButton.setTextColor( Color.YELLOW );
myButton.setGravity( Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL );
myButton.setText( "Hello World !" );

在布局中添加按钮:

layout.addView(myButton);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-27
    • 2020-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多