开发者

How to add multiple TextView s for a LinearLayout by Java Code ( in Android )

Here is my code. In the resulting screen it shows only the first TextView. Not the second one. Im kind of new to Android and please give a help.

public class Details extends Activity {
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        TextView label = new TextView(this);
        label.setText("Moves");
        label.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        label.setTextS开发者_如何转开发ize(20);        

        TextView label2 = new TextView(this);
        label2.setText("Time");
        label2.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        label2.setTextSize(20);       

        LinearLayout ll = new LinearLayout(this);
        ll.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
        ll.setOrientation(LinearLayout.VERTICAL);                           

        ll.addView(label);
        ll.addView(label2);

        setContentView(ll);
    }               
}


You forgot to tell the LinearLayout how to stack its children (Orientation). Without that it won't stack them at all and will ONLY display the first item (I believe).

http://developer.android.com/reference/android/widget/LinearLayout.html#setOrientation(int)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜