Android以编程方式添加页眉和页脚

时间:2016-09-07 11:44:42

标签: java android header footer

我有以下代码,我需要以编程方式添加"简单"使用相对布局修复标题(带图像)和页脚(带按钮):

/* Sample Application to create items, need a header image and footer with button*/

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.app.Activity;
    import android.graphics.Typeface;
    import android.os.Bundle;
    import android.view.View;
    import android.view.ViewGroup.LayoutParams;
    import android.view.Window;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.ImageView;
    import android.widget.CompoundButton;
    import android.widget.CompoundButton.OnCheckedChangeListener;
    import android.widget.LinearLayout;
    import android.widget.ScrollView;
    import android.widget.Toast;

    public class MainActivity extends Activity {

    ScrollView scrollview;
    Button dynamicbtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // requestWindowFeature(Window.FEATURE_NO_TITLE); //AppCompActivity on public class
        setContentView(R.layout.activity_main);

        scrollview = new ScrollView(this);
        LinearLayout linearlayout = new LinearLayout(this);
        linearlayout.setOrientation(LinearLayout.VERTICAL);
        scrollview.addView(linearlayout);

        ImageView imagetop = new ImageView(this);
        imagetop.setBackgroundResource(R.mipmap.ic_launcher );
        imagetop.setMaxHeight(50);
        imagetop.setMaxWidth(50);
        linearlayout.addView(imagetop);

        for(int i = 0; i<25;i++)
        {
            LinearLayout linear1 = new LinearLayout(this);
            linear1.setOrientation(LinearLayout.HORIZONTAL);
            linearlayout.addView(linear1);

            ImageView image = new ImageView(this);
            image.setBackgroundResource(R.mipmap.ic_launcher );
            image.setMaxHeight(50);
            image.setMaxWidth(50);
            linear1.addView(image);

            dynamicbtn = new Button(this);
            dynamicbtn.setText("Button no... "+i);
            dynamicbtn.setId(i);
            dynamicbtn.setTextSize(10);
            dynamicbtn.setPadding(8, 3, 8, 3);
            dynamicbtn.setTypeface(Typeface.SERIF,Typeface.BOLD_ITALIC);
            dynamicbtn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
            linear1.addView(dynamicbtn);
            dynamicbtn.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    Toast.makeText(getApplicationContext(), "Yipee.."+ v.getId(), Toast.LENGTH_SHORT).show();
                }
            });

            CheckBox dynamicchk = new CheckBox(this);
            dynamicchk.setId(i);
            dynamicchk.setText("Wow..A checkbox" + i);
            linear1.addView(dynamicchk);
            dynamicchk.setOnCheckedChangeListener(new OnCheckedChangeListener()
            {
                @Override
                public void onCheckedChanged(CompoundButton arg0, boolean arg1)
                {
                    Toast.makeText(getApplicationContext(), "Checked.."+ arg0.getId() + " " + arg1, Toast.LENGTH_SHORT).show();
                }
            });


        }

        this.setContentView(scrollview);

    }
}

0 个答案:

没有答案