[Android速通]简单控件和布局

/ 0评 / 0

这次开始拖拽测试各种控件,先打开activity_main.xml,这个页面是默认页面,上一节已经存在的了,然后切换到源码视图.

默认已经有一个TextView,我们尝试改一下.静态写法是这样的.

    <TextView
        android:id="@+id/tv_hello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="你好,世界." />

然而什么都没发生,原来是教程里是LinearLayout,先不管是什么,把这个布局拉过来.

<?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">

    <TextView
        android:id="@+id/tv_hello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="你好,世界." />

</LinearLayout>

如果用教程的带ActionBar的会发生错误Padding,所以就选没ActionBar,显示效果.

当然程序中可以写的,也能对显示内容设置.

tv_hello = findViewById(R.id.tv_hello);
tv_hello.setText("Hello World!");

就能看到文本改变了.

记得这里有一个警告,他建议我们把这些都塞到String资源里.

处理后是这样的.

字符除了内容,还可以设置大小,Android单位有3种,px,dp和sp,默认setTextSize方法单位是sp,也是推荐单位,他会根据系统设置的字体大小而一起改变.

tv_hello.setTextSize(30);

除了字体大小,还可以设置字体颜色,颜色是ARGB,比如0x00FF0000是透明的红色,自然什么都看不到,还内置了很多预制颜色.

tv_hello.setTextColor(Color.BLUE);

就像字符串资源一样,颜色也会在colors.xml预定义,这样也可以在setTextColor引用.

tv_hello.setTextColor(R.color.black);

同理还有setBackgroundColor这里就不展开讲了,再说就很像写手册了.

前面说控件的时候,提到了一个概念,视图,而前面用的是线性视图,也只放了一个元素,他自然的就放在左上角靠着了.而且每个控件都有自己的layout_width和layout_height,也是需要设置的.

前面说的,布局里的单位是dp,而设置属性的参数单位是px,所以还需要涉及到转换.

public class Utils {
    // 根据手机的分辨率从 dp 的单位 转成为 px(像素)
    public static int dip2px(Context context, float dpValue) {
        // 获取当前手机的像素密度(1个dp对应几个px)
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f); // 四舍五入取整
    }

    // 根据手机的分辨率从 px(像素) 的单位 转成为 dp
    public static int px2dip(Context context, float pxValue) {
        // 获取当前手机的像素密度(1个dp对应几个px)
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f); // 四舍五入取整
    }
}

然后通过这个方法修改

ViewGroup.LayoutParams layoutParams = tv_hello.getLayoutParams();
layoutParams.width = Utils.dip2px(this,300);
tv_hello.setLayoutParams(layoutParams);

两个标签之间可以通过layout_margin修改.

关于布局上说的也太多了,再继续说,也是直接复制文档了.下面进行一些总结.

LinearLayout (线性布局)

1. 基本概念

2. orientation 属性

3. weight 权重

  <LinearLayout android:orientation="horizontal" ...>
      <TextView android:layout_weight="1" ... />
      <TextView android:layout_weight="2" ... />
  </LinearLayout>

4. 其他常用属性

RelativeLayout (相对布局)

1. 基本概念

2. 常用布局属性(在子元素中设置)

3. 示例

<RelativeLayout ...>
    <TextView
        android:id="@+id/title"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_below="@id/title"
        android:layout_alignParentRight="true" />
</RelativeLayout>

GridLayout (网格布局)

1. 基本概念

2. 属性(父布局)

3. 属性(子元素)

4. 示例

<GridLayout
    android:columnCount="2"
    android:rowCount="2" ... >

    <Button
        android:text="A"
        app:layout_row="0"
        app:layout_column="0" />

    <Button
        android:text="B"
        app:layout_row="0"
        app:layout_column="1" />
</GridLayout>

ScrollView (滚动视图)

1. 基本概念

2. 垂直滚动:ScrollView

<ScrollView>
    <LinearLayout android:orientation="vertical">
        <!-- 多个子控件 -->
    </LinearLayout>
</ScrollView>

3. 水平滚动:HorizontalScrollView

<HorizontalScrollView>
    <LinearLayout android:orientation="horizontal">
        <!-- 多个子控件 -->
    </LinearLayout>
</HorizontalScrollView>

4. 常用属性

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注