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

时间:2014-02-09 13:44:29

标签: android android-layout

您好,我是Android开发的初学者。 我已经制作了一个LinearLayout,以一种很好的方式显示不同的信息。 我想要一个带ScrollView的屏幕,我可以在其中添加我想要的尽可能多的LinearLayouts。 但问题是,Button事件不会添加任何LinearLayouts ......

这是我的activity_layouttest.xml:


RelativeLayout的

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

-----的LinearLayout

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

我希望你能找到丢失的Command或者其他的。 感谢

1 个答案:

答案 0 :(得分:1)

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