如何在Android的滚动视图中添加多个按钮

3
我希望实现这个功能: 动态向滚动视图添加多个按钮,如果滚动视图高度超过一定值,则会自动显示滚动条。
你能给我一些建议吗?

你的问题不够清晰。最好是展示一下你所尝试过的内容。 - Kanth
5个回答

8

请查看以下代码片段:

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

引用自如何动态向使用XML创建的视图中添加元素


谢谢您的建议。但我想要添加更多横向排列的按钮,并且如果超过一行,它将自动换行。您能再帮我一次吗? - Kevin
那么,如果您想要使项目自适应,为什么需要一个ScrollView呢? - Ahmad Kayyali
谢谢!这是我的要求。我使用 http://code.google.com/p/android-masonry/ 上的开源代码解决了这个问题。我是一名中国新手 Android 开发者,再次感谢你的帮助。 - Kevin
我正在尝试在上述过程中动态添加按钮,但是我无法看到该按钮。有人能给出任何提示为什么会发生这种情况吗? - user1859771
LinearLayout.VERTICAL 是你想要的吗?尝试使用 LinearLayout.HORIZONTAL。 - Ahmad Kayyali

2
将比如说一个线性布局放在滚动视图中,然后将按钮添加到线性布局中。问题得以解决。

1
这是一个关于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);


0

由于ScrollView只能托管一个直接子元素。您可以将一个LinearLayout添加到ScrollView中,并以编程方式向该LinearLayout添加按钮。


0

这是我的布局文件示例:

  <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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接