3

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

你能给我一些建议吗?

4

5 回答 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);

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

于 2013-02-17T11:35:31.757 回答
2

例如在滚动视图中放置一个线性布局,然后将按钮添加到线性布局。问题解决了。

于 2013-02-17T11:35:13.133 回答
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);

于 2018-05-16T15:31:11.123 回答
0

由于滚动视图只能托管一个直接子级。您可以添加 a并linearLayoutScrollView编程方式向其中添加按钮LinearLayout

于 2013-02-17T11:35:10.480 回答
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>
于 2016-05-19T10:57:17.423 回答