Android利用setLayoutParams在代码中调整布局(Margin和居中)
来源:互联网 发布:linux 设置ftp 编辑:程序博客网 时间:2024/05/20 14:40
MainActivity如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package cn.testfixmargin;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.app.Activity;
/**
* Demo描述:
* 在代码中设置布局的属性
* 比如Margin和居中
*
* 注意事项:
* 参见代码中的详细注释
*/
public class MainActivity extends Activity {
private TextView mTextView;
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init(){
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
float density=displayMetrics.density;
float fontScale = displayMetrics.scaledDensity;
System.out.println("density="+density+",fontScale="+fontScale);
mTextView=(TextView) findViewById(R.id.textView);
mButton=(Button) findViewById(R.id.button);
mButton.setOnClickListener(new OnClickListenerImpl());
}
private class OnClickListenerImpl implements OnClickListener {
@Override
public void onClick(View v) {
// //--------以下为测试1 在代码中为控件设置Margin--------
// //注意:
// //1 此处的new RelativeLayout.LayoutParams(int w, int h)参数w,h指的是
// // 该控件的父控件的在布局文件中所设置的宽和高
// //2 此处必须使用RelativeLayout.LayoutParams.FILL_PARENT()
// // 因为其父类为RelativeLayout所以是其父类的布局参数即RelativeLayout.LayoutParams.XXX
// // 注意其官方文档的描述:
// // Set the layout parameters associated with this view.
// // These supply parameters to the parent of this view specifying how it should be arranged.
// // 也就是说这个setLayoutParams()是给其父控件看的
// // 其实这也好理解:只有父类可以改变子View的位置布局.而不是说子View可以随意
// // 按照自己的想法摆放自己的位置,而不受父控件控制
// RelativeLayout.LayoutParams layoutParams
// =new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
// layoutParams.setMargins(280, 0, 0, 0);
// mTextView.setLayoutParams(layoutParams);
// //--------以上为测试1--------
//--------以下为测试2 在代码中设置控件居中--------
//注意:
//1 此处的new RelativeLayout.LayoutParams(int w, int h)参数w,h指的是
// 该控件在布局文件中所设置的宽和高
//2 同测试1中的描述
RelativeLayout.LayoutParams layoutParams=
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
mTextView.setLayoutParams(layoutParams);
//--------以下为测试2--------
}
}
}
package cn.testfixmargin;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.app.Activity;
/**
* Demo描述:
* 在代码中设置布局的属性
* 比如Margin和居中
*
* 注意事项:
* 参见代码中的详细注释
*/
public class MainActivity extends Activity {
private TextView mTextView;
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init(){
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
float density=displayMetrics.density;
float fontScale = displayMetrics.scaledDensity;
System.out.println("density="+density+",fontScale="+fontScale);
mTextView=(TextView) findViewById(R.id.textView);
mButton=(Button) findViewById(R.id.button);
mButton.setOnClickListener(new OnClickListenerImpl());
}
private class OnClickListenerImpl implements OnClickListener {
@Override
public void onClick(View v) {
// //--------以下为测试1 在代码中为控件设置Margin--------
// //注意:
// //1 此处的new RelativeLayout.LayoutParams(int w, int h)参数w,h指的是
// // 该控件的父控件的在布局文件中所设置的宽和高
// //2 此处必须使用RelativeLayout.LayoutParams.FILL_PARENT()
// // 因为其父类为RelativeLayout所以是其父类的布局参数即RelativeLayout.LayoutParams.XXX
// // 注意其官方文档的描述:
// // Set the layout parameters associated with this view.
// // These supply parameters to the parent of this view specifying how it should be arranged.
// // 也就是说这个setLayoutParams()是给其父控件看的
// // 其实这也好理解:只有父类可以改变子View的位置布局.而不是说子View可以随意
// // 按照自己的想法摆放自己的位置,而不受父控件控制
// RelativeLayout.LayoutParams layoutParams
// =new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
// layoutParams.setMargins(280, 0, 0, 0);
// mTextView.setLayoutParams(layoutParams);
// //--------以上为测试1--------
//--------以下为测试2 在代码中设置控件居中--------
//注意:
//1 此处的new RelativeLayout.LayoutParams(int w, int h)参数w,h指的是
// 该控件在布局文件中所设置的宽和高
//2 同测试1中的描述
RelativeLayout.LayoutParams layoutParams=
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
mTextView.setLayoutParams(layoutParams);
//--------以下为测试2--------
}
}
}
main.xml如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<
RelativeLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
xmlns:tools
=
"http://schemas.android.com/tools"
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
>
<
TextView
android:id
=
"@+id/textView"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:text
=
"@string/hello_world"
android:textSize
=
"25sp"
android:layout_marginLeft
=
"20dip"
/>
<
Button
android:id
=
"@+id/button"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:text
=
"Click"
android:textSize
=
"25sp"
android:layout_centerInParent
=
"true"
/>
</
RelativeLayout
>
<
RelativeLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
xmlns:tools
=
"http://schemas.android.com/tools"
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
>
<
TextView
android:id
=
"@+id/textView"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:text
=
"@string/hello_world"
android:textSize
=
"25sp"
android:layout_marginLeft
=
"20dip"
/>
<
Button
android:id
=
"@+id/button"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:text
=
"Click"
android:textSize
=
"25sp"
android:layout_centerInParent
=
"true"
/>
</
RelativeLayout
>
0 0
- 利用setLayoutParams在代码中调整布局(Margin和居中)
- Android利用setLayoutParams在代码中调整布局(Margin和居中)
- Android利用setLayoutParams在代码中调整布局(Margin和居中)
- Android利用setLayoutParams在代码中调整布局(Margin和居中)
- Android开发布局中margin和padding的区别以及文本的快速居中的方法
- Android在java代码中设置margin
- Android在java代码中设置margin
- Android在java代码中设置margin
- android如何在代码中设置margin
- android在代码中设置margin
- margin布局居中显示
- android tabwidget 调整布局高度 文字居中
- android的布局属性中padding和margin的区别
- Android的布局属性中padding和margin的区别
- android布局中有关margin和padding的区别
- android布局中margin和padding的区别!
- Android布局中margin,padding,align的用法和区别
- 在代码中设置margin
- python 去扫域名是否被注册
- [MarsZ]程序猿谈大学之为什么不推荐就业时做程序猿
- 算法设计与分析:第四章 动态规划 4.6仪器维修时间表问题
- 【Qt OpenGL教程】02:你的第一个多边形
- js 函数式编程 浅谈
- Android利用setLayoutParams在代码中调整布局(Margin和居中)
- 黑马程序员——初入Java
- Eclipse 常用快捷键
- [渗透测试] ARP欺骗攻击&DNS欺骗攻击
- (2) Servlet - 生命周期,工作方式
- HashMap 与 Hashtable对比
- Android Camera设置setPreviewCallback实现onPreviewFrame接口实时截取每一帧视频流数据
- 算法设计与分析:第五章 回溯法 5.1TSP之货郎担问题
- Atom飞行手册翻译: 2.6 代码段