android代码创建布局
来源:互联网 发布:菊水 清酒 知乎 编辑:程序博客网 时间:2024/06/08 14:00
效果图:
布局代码:
<?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); }}
0 0
- android代码创建布局
- 【Android】使用代码动态创建布局
- android使用java代码创建控件、布局
- android 相对布局,代码创建imageview,布局居中问题
- 代码创建布局约束
- 代码创建布局
- 动态代码创建布局
- Android 动态布局 (代码布局)
- Android创建布局按钮
- Android中不使用布局文件,java代码动态创建布局及控件
- Android布局java代码
- [Android]代码控制布局
- android代码布局
- android 代码布局 记录
- android代码布局错误
- Android代码布局
- android中的代码布局
- 【Android】代码实现布局
- Dynamics CRM2015 非基础语言环境下产品无法新建的问题
- Android如何设置圆角按钮
- android <viewStub />, <requestFocus />, <merge /> and <include />用法
- Codeforces Round #279 (Div. 2) B. Queue 模拟
- 初学者很实用:纯struts框架实现用户登陆
- android代码创建布局
- HTMLParser 学习篇上
- OC类之 Dictionary
- 自定义Dialog
- ADB端口被占用,adb server is out of date
- Flying to the Mars
- Linux下新建分区步骤
- [DLX精确覆盖] hdu 1603 A Puzzling Problem
- python scrapy 爬虫 未完待续