添加新视角布局一旦按钮单击安卓 [英] Add New View to Layout Upon Button Click in Android

查看:153
本文介绍了添加新视角布局一旦按钮单击安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个记分申请的卡片游戏黑桃。

I'm developing a Scorekeeping Application for the card game "Spades".

一旦点击一个按钮每手结束,我想关于手的一些信息存储到一个TextView,并在滚动型显示所有的牌局历史的。

Upon a button click at the end of each hand, I would like to store some information about the hand into a TextView and display all of the hand histories in a ScrollView.

我如何修改.XML布局或增加新的TextView在code进行布局?

How can I modify an .XML layout or otherwise add a new TextView to a layout in code?

我已经试过......

I have tried...

public static LinearLayout gamehistory;

gamehistory = (LinearLayout) findViewById(R.id.gamehistory);


public void setClickListener1(){

lockwagersButton.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

setContentView(R.layout.results_layout);

gameHistory.addView(new TextView(...));       //  I can't get this to work 
            ...

谢谢! -K.H。

Thanks! -K.H.

推荐答案

以下code添加的TextView的onclick:

following code add textview onclick:

public class ViewOnClick extends Activity {
    LinearLayout.LayoutParams layoutParams;
    LinearLayout ll;
    static int i;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button b = (Button)findViewById(R.id.Button01);
        ll = (LinearLayout)findViewById(R.id.ll);
        layoutParams = new LinearLayout.LayoutParams
        (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        b.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                TextView view = new TextView(ViewOnClick.this);             
                view.setText(++i+" view");
                ll.addView(view, layoutParams); 

            }
        });
    }
}

这篇关于添加新视角布局一旦按钮单击安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆