android中LayoutParams
来源:互联网 发布:教师网络信息管理平台 编辑:程序博客网 时间:2024/06/08 11:26
可以这样去形容LayoutParams,在象棋的棋盘上,每个棋子都占据一个位置,也就是每个棋子都有一个位置的信息,如这个棋子在4行4列,这里的“4行4列”就是棋子的LayoutParams。
但LayoutParams类也只是简单的描述了宽高,宽和高都可以设置成三种值:
1,一个确定的值;
2,FILL_PARENT,即填满(和父容器一样大小);
3,WRAP_CONTENT,即包裹住组件就好。
父布局下的子控件要设置
因为布局很多,虽然都继承至ViewGroup但是各个布局还是有很大的不同。
很显然上面这句应该这样写才算准确:
setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.FILL_PARENT));
这表示这个子控件的父布局是一个TableRow , 这样的LayoutParams 太多,所以应明确指明。
1. FrameLayout下动态设置子控件居中,动态用JAVA代码要这样实现:
FrameLayout.LayoutParams lytp = new FrameLayout.LayoutParams(80,LayoutParams.WRAP_CONTENT);
lytp .gravity = Gravity.CENTER;
btn.setLayoutParams(lytp);
2. RelativeLayout下动态设置子控件居中:
RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
btn1.setLayoutParams(lp);
public class Test extends Activity { LinearLayout a; int aaa; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test); a=(LinearLayout)findViewById(R.id.root); TextView b=new TextView(this); b.setText("ssss"); LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); params.gravity= Gravity.CENTER; b.setGravity(Gravity.CENTER);//<span style="color:#ff0000;">需要注意的是:设置gravity需要在具体控件上设置,而不是给params设置</span> b.setLayoutParams(params); a.addView(b); }
</pre><pre code_snippet_id="1760520" snippet_file_name="blog_20160713_3_3878264" name="code" class="java"><pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:id="@+id/root" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="XML" /> </LinearLayout></LinearLayout>
<span style="font-size:18px;color:#ff0000;">//是什么.<span style="font-family: Arial, Helvetica, sans-serif;">LayoutParams取决于父布局,如果是最外层则要要用FragmentLout。</span><span style="font-family: Arial, Helvetica, sans-serif;">LayoutParams(实际测试,暂不清楚原因)</span></span>
- android中LayoutParams
- Android中LayoutParams总结
- Android中LayoutParams的用法
- Android中LayoutParams的用法
- Android中LayoutParams的用法
- Android中LayoutParams的用法
- Android中LayoutParams的用法
- Android中LayoutParams的用法
- Android中LayoutParams的用法
- Android中LayoutParams的用法
- Android中LayoutParams的用法
- Android中LayoutParams的用法
- Android中LayoutParams的使用
- Android中WindowManager.LayoutParams类详解
- Android中LayoutParams总结和用法
- Android中WindowManager.LayoutParams类详解
- Android中LayoutParams总结和用法
- Android开发中WindowManager.LayoutParams详解
- git是什么?如何使用git进行团队协作?
- 【maven 架构】 pom.xml 配置详解
- 在anaconda python开发套件下进行opencv的安装
- hdu 5475 An easy problem(线段树单点更新)
- mysql 的存储过程
- android中LayoutParams
- Java IO - PipedOutputStream & PipedInputStream
- XTU 程序设计实践模拟考试题1
- Linux异步通知fasync
- MyEclipse 2014优化设置
- 利用Python实现Fibonacci Squence
- MongoDB的性能优化
- 几个南阳oj的STL题
- 机器学习之线性回归