Android入门之视图操作
Android入门之视图操作
Android入门之视图操作
文本设置操作
1 2 3 4 5
| TextView fooText = findViewById(R.id.foo_text);
fooText.setText(R.string.foo_new_text1); fooText.setTextSize(50);
|
设置文本颜色
1
| <TextView android:textColor="#90EE90" />
|
1 2 3 4 5 6 7 8 9
| fooText.setTextColor(0xFFFFFF); fooText.setTextColor(Color.RED); fooText.setTextColor(Color.parseColor("#FF6B81")); fooText.setTextColor(Color.rgb(255,255,255)); fooText.setTextColor(getResources().getColor(R.color.fei_watermelon));
fooText.setBackgroundColor(Color.parseColor("#00FFFF")); fooText.setBackgroundColor(getResources().getColor(R.color.fei_color_aqua));
|
视图的宽高
1 2 3 4 5 6 7
| TextView fooText02 = findViewById(R.id.foo_text02);
ViewGroup.LayoutParams params = fooText02.getLayoutParams();
params.width = 100; fooText02.setLayoutParams(params);
|
视图的间距
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| <?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="300dp" android:background="#c0c0c0" android:orientation="vertical">
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="20dp" android:background="#90ee90" android:padding="60dp">
<View android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff6b81"/>
</LinearLayout>
</LinearLayout>
|

视图的对齐
1 2 3 4 5 6
| 01) 设置视图的对齐方式有两种途径 01-1)采用 layout_gravity 属性,它指定了当前视图相对于**上级视图**的对齐方式 01-2)采用 gravity 属性,它指定了**下级视图**相对于视图的对齐方式
02)layout_gravity 与 gravity 的取值包括: left、right、top、bottom, 还可以用竖线 连接各取值,例如"left|top"表示即靠左又靠上,也就是朝左上角对齐
|
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
| <?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="300dp" android:background="#c0c0c0" android:orientation="horizontal">
<LinearLayout android:layout_width="0dp" android:layout_height="200dp" android:layout_gravity="bottom" android:layout_margin="10dp" android:layout_weight="1" android:background="#90ee90" android:gravity="left" android:padding="10dp">
<View android:layout_width="100dp" android:layout_height="100dp" android:background="#ff6b81"/>
</LinearLayout>
<LinearLayout android:layout_width="0dp" android:layout_height="200dp" android:layout_gravity="top" android:layout_margin="10dp" android:layout_weight="1" android:background="#90ee90" android:gravity="right" android:padding="10dp">
<View android:layout_width="100dp" android:layout_height="100dp" android:background="#ff6b81"/>
</LinearLayout>
</LinearLayout>
|

底部
没有了