【问题标题】:How to add more than one button to scrollview in android如何在android中向scrollview添加多个按钮
【发布时间】:2013-02-17 11:23:15
【问题描述】:

我想实现这个功能:动态添加多个按钮到scrollview,如果scrollview超过一定高度,就会自动显示滚动条。

你能给我一些建议吗?

【问题讨论】:

  • 您的问题不清楚。最好展示一下您的尝试。

标签: android button scrollview


【解决方案1】:

检查以下代码sn-p:

// Find the ScrollView 
ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView1);

// Create a LinearLayout element
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);

// Add Buttons
Button button = new Button(this);
button.setText("Some text");
linearLayout.addView(button);

// Add the LinearLayout element to the ScrollView
scrollView.addView(linearLayout);

引用自How do I add elements dynamically to a view created with XML

【讨论】:

  • 感谢您的建议。但我想通过水平添加更多按钮,如果超过一行,它将自动换行。你能再帮我一次吗?
  • 那么,如果您想包装商品,为什么需要ScrollView
  • 非常感谢。这是要求。我已经从code.google.com/p/android-masonry 解决了这个问题参考开源代码。我是一名中国新晋安卓开发者,再次感谢您的帮助。
  • 我正在尝试在上面给出的过程中动态添加按钮。但我无法查看按钮。任何人都可以给出任何提示为什么它会发生..
  • LinearLayout.VERTICAL 是你想要的吗?试试 LinearLayout.HORIZONTAL
【解决方案2】:

例如在 scrollview 中放置一个 linearlayout,然后将按钮添加到 linearlayout。问题解决了。

【讨论】:

    【解决方案3】:

    这是 Xamarin C# 的示例。

    <ContentPage.Content>
        <ScrollView x:Name="ScrollLogonID"                     
            BackgroundColor="Transparent"
            HorizontalOptions="FillAndExpand" Opacity="0.85">
            <StackLayout Opacity="0.85">
                <StackLayout 
                    x:Name="LogoID" 
                    BackgroundColor="Transparent"
                    VerticalOptions="Start" 
                    Opacity="0.65"
                    HorizontalOptions="FillAndExpand">
                        <!--- place the button here -->
                </StackLayout> 
            </StackLayout> 
        </ScrollView>
    </ContentPage.Content>
    <!--
    /*
    C# Code snippet
    */-->
        Xamarin.Forms.Button btnEnter = new Xamarin.Forms.Button {
            Text = "Entrar",
            HorizontalOptions = LayoutOptions.FillAndExpand,                
            FontSize = 18,
            HeightRequest = 26,
            TextColor = Color.LightGray,
            BackgroundColor = Color.DarkRed
        };
    
        StackLayout stkButton = new StackLayout{ Children = { btnEnter }};
        this.FindByName<StackLayout>("LogonID").Children.Add(stkButton);
    

    【讨论】:

      【解决方案4】:

      因为滚动视图只能承载一个直接子级。您可以将 linearLayout 添加到 ScrollView 并以编程方式将按钮添加到 LinearLayout

      【讨论】:

        【解决方案5】:

        这是我在布局文件中的示例:

          <ScrollView
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:id="@+id/scrollView" >
        
                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">
        
                        <Button
                            android:layout_width="fill_parent"
                            android:layout_height="120dp"
                            android:id="@+id/button4" />
        
                        <Button
                            android:layout_width="fill_parent"
                            android:layout_height="120dp"
                            android:id="@+id/button" />
        
                        <Button
                            android:layout_width="fill_parent"
                            android:layout_height="120dp"
                            android:id="@+id/button9" />
        
                        <Button
                            android:layout_width="fill_parent"
                            android:layout_height="120dp"
                            android:id="@+id/button10" />
                    </LinearLayout>
           </ScrollView>
        

        【讨论】:

          猜你喜欢
          • 2021-07-09
          • 1970-01-01
          • 1970-01-01
          • 2018-03-04
          • 1970-01-01
          • 1970-01-01
          • 2021-11-24
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多