gpt4 book ai didi

android - 动态创建按钮的垂直 ScrollView

转载 作者:太空狗 更新时间:2023-10-29 16:18:52 24 4
gpt4 key购买 nike

我在动态创建的按钮中添加 ScrollView 时遇到问题。这是我的 xml 代码。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<LinearLayout
android:id="@+id/linear1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="27dp"
android:background="@drawable/custom_border"
android:orientation="vertical" >

<EditText
android:id="@+id/editText1"
android:layout_width="243dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/linearLayout1"
android:layout_marginTop="28dp"
android:ems="10"
android:hint="@string/hinttextitem" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/editText1"
android:layout_alignParentRight="true"
android:layout_marginRight="26dp"
android:background="@drawable/button_shape"
android:onClick="newonClick"
android:text="click" />
</LinearLayout>


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/linear1"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:id="@+id/dynamic"
android:background="@drawable/dynamic_border"
android:orientation="vertical" >
</LinearLayout>

</RelativeLayout>

这是我的java代码

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void newonClick(View V){
LinearLayout layout=(LinearLayout)findViewById(R.id.linear1);
LinearLayout dynamiclayout=(LinearLayout)findViewById(R.id.dynamic);
dynamiclayout.removeAllViews();
EditText edittext = (EditText) findViewById(R.id.editText1);
int x=Integer.valueOf(edittext.getText().toString());
for(int i=1;i<=x;i++)
{
Button addButton =new Button(this);
addButton.setText("Button"+i);
addButton.setId(i);
dynamiclayout.addView(addButton);
addButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(),
"button is clicked"+v.getId(), 8000).show();
}
});
}

如果我在动态布局中添加 ScrollView ,它将消失。那么我如何才能只为动态创建的按钮添加垂直 ScrollView 。有人能解决我的问题吗?

最佳答案

试试这个:只为 LinearLayout 放置 ScrollView 。

 <ScrollView
android:layout_width="wrap_content"
android:layout_below="@+id/linear1"
android:layout_height="wrap_content" >

<LinearLayout
android:id="@+id/dynamic"
android:layout_width="match_parent"
android:layout_height="match_parent"

android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:background="@drawable/dynamic_border"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>

关于android - 动态创建按钮的垂直 ScrollView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20770942/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com