gpt4 book ai didi

android - 如何将新的 LinearLayout 添加到现有的 LinearLayout 中?

转载 作者:行者123 更新时间:2023-11-30 03:06:52 27 4
gpt4 key购买 nike

您好,我是 Android 开发的初学者。我制作了一个 LinearLayout 以美观的方式显示不同的信息。我想要一个带有 ScrollView 的屏幕,我可以在其中添加任意数量的 LinearLayouts。但问题是 Button 事件没有添加任何 LinearLayouts...

这是我的activity_layouttest.xml:


相对布局

-----按钮@+id/spawnbutton

-----线性布局

------------ ScrollView @+id/scrollView1

--------------------LinearLayout: spawnLayout


这是我的代码:


public class LayoutTest extends Activity {


// declaring Widgets
LinearLayout SpawnLayout;
Button Spawnbtn;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_layouttest);

Spawnbtn = (Button) findViewById(R.id.spawnbutton);
SpawnLayout = new LinearLayout(readtest.this);
// set On Click Listener to a Button "Spawnbtn which schould add the new
// Layout to the existion one

Spawnbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dospawn();
}
});
}


// method to create/add new linearLayout


public void dospawn() {



// create custom Layout components
String LEHRER = "Lehrer";
String STUNDEN = "Stunden";
String KLASSE = "Klasse";
String ORT = "Ort";
String INFO = "Info";
String FACH = "Fach";


//create new objects
LinearLayout linlay = new LinearLayout(CONTEXT);
TextView tvLehrer = new TextView(CONTEXT);
TextView tvStunden = new TextView(CONTEXT);
TextView tvKlasse = new TextView(CONTEXT);
TextView tvOrt = new TextView(CONTEXT);
TextView tvInfo = new TextView(CONTEXT);
TextView tvFach = new TextView(CONTEXT);


//defining each one including the child - parent Layout "linlay"


linlay.setOrientation(LinearLayout.VERTICAL);
linlay.setPadding(20, 10, 20, 10);
linlay.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
Resources res = CONTEXT.getResources();
tvLehrer.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));



tvLehrer.setBackgroundColor(res.getColor(R.color.transparent_light));
tvLehrer.setGravity(Gravity.RIGHT);
tvLehrer.setPadding(0, 0, 20, 0);
tvLehrer.setText(LEHRER);
tvLehrer.setTextColor(res.getColor(R.color.weiss));
tvLehrer.setTextSize(20);



tvStunden.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
tvStunden.setBackgroundColor(res.getColor(R.color.transparent_light));
tvStunden.setGravity(Gravity.LEFT);
tvStunden.setPadding(20, 0, 0, 0);
tvStunden.setText(STUNDEN);
tvStunden.setTextColor(res.getColor(R.color.weiss));



tvKlasse.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
tvKlasse.setBackgroundColor(res.getColor(R.color.transparent_light));
tvKlasse.setGravity(Gravity.LEFT);
tvKlasse.setPadding(20, 0, 0, 0);
tvKlasse.setText(KLASSE);
tvKlasse.setTextColor(res.getColor(R.color.weiss));


tvOrt.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
tvOrt.setBackgroundColor(res.getColor(R.color.transparent_light));
tvOrt.setGravity(Gravity.LEFT);
tvOrt.setPadding(20, 10, 0, 10);
tvOrt.setText(ORT);
tvOrt.setTextColor(res.getColor(R.color.weiss));



tvInfo.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
tvInfo.setBackgroundColor(res.getColor(R.color.transparent_light));
tvInfo.setGravity(Gravity.LEFT);
tvInfo.setPadding(20, 0, 0, 10);
tvInfo.setText(INFO);
tvInfo.setTextColor(res.getColor(R.color.weiss));



tvFach.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
tvFach.setBackgroundColor(res.getColor(R.color.orange));
tvFach.setGravity(Gravity.CENTER_HORIZONTAL);
tvFach.setPadding(0, 10, 0, 10);
tvFach.setText(FACH);
tvFach.setTextColor(res.getColor(R.color.schwarz));



//add the Components to LinearLayout linlay



linlay.addView(tvLehrer);
linlay.addView(tvStunden);
linlay.addView(tvKlasse);
linlay.addView(tvOrt);
linlay.addView(tvInfo);
linlay.addView(tvFach);

// add the linlay to the LinearLayout which is defined in the activity.

SpawnLayout.addView(linlay);
}

我希望你能找到丢失的命令或其他东西。谢谢

最佳答案

您的 Activity 代码没有任何 ScrollView 。请粘贴 Activity 布局的内容(即 activity_layoutTest.xml)。查看代码,我认为,您最好将滚动条与列表适配器一起使用。看看这个问题可能对你有帮助:Using a ListAdapter to fill a LinearLayout inside a ScrollView layout

关于android - 如何将新的 LinearLayout 添加到现有的 LinearLayout 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21659778/

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