android代码创建布局

本文介绍了一个具体的Android应用布局案例,使用RelativeLayout实现了一个包含图片、文本和按钮的界面,并通过代码详细展示了如何设置布局参数及组件属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果图:

这里写图片描述

布局代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/ll_view"
              xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical">
</RelativeLayout>

页面代码:

public class ViewActivity extends Activity {
    private RelativeLayout mLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view);

        mLayout = $(R.id.ll_view);
        mLayout.setBackgroundColor(Color.GREEN);
        //单独设置属性
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(mLayout.getLayoutParams());
        lp.setMargins(0, 100, 0, 0);
        mLayout.setLayoutParams(lp);

        ImageView imageView = new ImageView(this);
        imageView.setId(getResources().getInteger(R.integer.one));
        imageView.setImageDrawable(getResources().getDrawable(R.drawable.smile));
        imageView.setBackgroundColor(Color.MAGENTA);
        imageView.setPadding(20, 20, 20, 20);
        mLayout.addView(imageView);

        TextView textView = new TextView(this);
        textView.setId(getResources().getInteger(R.integer.two));
        textView.setBackgroundColor(Color.LTGRAY);
        textView.setPadding(20, 0, 20, 20);
        textView.setText("藏友号:54402");
        textView.setTextSize(16);
        //设置布局属性
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_TOP, getResources().getInteger(R.integer.one));
        params.addRule(RelativeLayout.RIGHT_OF, getResources().getInteger(R.integer.one));
        mLayout.addView(textView, params);

        Button button = new Button(this);
        button.setId(getResources().getInteger(R.integer.three));
        button.setBackgroundColor(Color.CYAN);
        button.setPadding(20, 0, 20, 0);
        button.setText("+ 关注");
        button.setTextSize(16);
        //设置布局属性
        RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params1.addRule(RelativeLayout.BELOW, getResources().getInteger(R.integer.two));
        params1.addRule(RelativeLayout.RIGHT_OF, getResources().getInteger(R.integer.one));
        params1.addRule(RelativeLayout.ALIGN_BOTTOM, getResources().getInteger(R.integer.one));
        mLayout.addView(button, params1);

    }

    public <T> T $(int viewID) {
        return (T) findViewById(viewID);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值