TextView01

TextView01

1、TextView代码展示

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:text="吴豪乐工作室" 
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:id="@+id/whl_one"
        android:layout_width="200dp"
        android:layout_height="200dp" />
    
</LinearLayout>

(1)首先linerLayout为一个容器,TextView是包含在容器里面的一个控件
(2)其中layout_width按住ctl+鼠标左键进去,发现有两个值可以选择“match_parent”(跟容器宽度值相等)和"wrap_content"(根据容器内容分配大小)或者直接输入数字“200dp”表示200个像素的宽度。

<attr name="layout_width" format="dimension">
            <!-- The view should be as big as its parent (minus padding).
                 This constant is deprecated starting from API Level 8 and
                 is replaced by {@code match_parent}. -->
            <enum name="fill_parent" value="-1" />
            <!-- The view should be as big as its parent (minus padding).
                 Introduced in API Level 8. -->
            <enum name="match_parent" value="-1" />
            <!-- The view should be only big enough to enclose its content (plus padding). -->
            <enum name="wrap_content" value="-2" />
        </attr>

(3)id表示这个TextView的名字是叫什么,用于识别TextView的,格式如下

android:id="@+id/whl_one"

在我们的java代码中可以使用如下代码可以获取得到我们的TextView对象

TextView myTextView = findViewById(R.id.whl_one);
myTextView.setText("你好!");

(4)text属性表示在textview中显示的文本

android:text="吴豪乐工作室" //非正规用法
android:text="@string/tv_one" //正规用法

在string.xml中定义如下:

<resources>
    <string name="app_name">My Application</string>
    <string name="tv_one">吴豪乐工作室</string>
</resources>

也可以在javad代码中使用如下代码将其需要显示的内容进行覆盖

 myTextView.setText("你好!");

(5)textcolor表示文本的颜色

android:textColor="#F44336"

(6)textstyle表示文本风格

    <attr name="textStyle">
        <flag name="normal" value="0" /> //正常
        <flag name="bold" value="1" />//加粗
        <flag name="italic" value="2" />//斜体字
    </attr>

(7)textsize文本字体大小

 android:textSize="60sp"/>

(8)背景颜色background

 android:background="#FF00F00"

(9)布局gravity

<attr name="gravity">
        <!-- Push object to the top of its container, not changing its size. -->
        <flag name="top" value="0x30" />
        <!-- Push object to the bottom of its container, not changing its size. -->
        <flag name="bottom" value="0x50" />
        <!-- Push object to the left of its container, not changing its size. -->
        <flag name="left" value="0x03" />
        <!-- Push object to the right of its container, not changing its size. -->
        <flag name="right" value="0x05" />
        <!-- Place object in the vertical center of its container, not changing its size. -->
        <flag name="center_vertical" value="0x10" />
        <!-- Grow the vertical size of the object if needed so it completely fills its container. -->
        <flag name="fill_vertical" value="0x70" />
        <!-- Place object in the horizontal center of its container, not changing its size. -->
        <flag name="center_horizontal" value="0x01" />
        <!-- Grow the horizontal size of the object if needed so it completely fills its container. -->
        <flag name="fill_horizontal" value="0x07" />
        <!-- Place the object in the center of its container in both the vertical and horizontal axis, not changing its size. -->
        <flag name="center" value="0x11" />
        <!-- Grow the horizontal and vertical size of the object if needed so it completely fills its container. -->
        <flag name="fill" value="0x77" />
        <!-- Additional option that can be set to have the top and/or bottom edges of
             the child clipped to its container's bounds.
             The clip will be based on the vertical gravity: a top gravity will clip the bottom
             edge, a bottom gravity will clip the top edge, and neither will clip both edges. -->
        <flag name="clip_vertical" value="0x80" />
        <!-- Additional option that can be set to have the left and/or right edges of
             the child clipped to its container's bounds.
             The clip will be based on the horizontal gravity: a left gravity will clip the right
             edge, a right gravity will clip the left edge, and neither will clip both edges. -->
        <flag name="clip_horizontal" value="0x08" />
        <!-- Push object to the beginning of its container, not changing its size. -->
        <flag name="start" value="0x00800003" />
        <!-- Push object to the end of its container, not changing its size. -->
        <flag name="end" value="0x00800005" />
    </attr>
上一篇:安卓开发


下一篇:Android 神奇的SpannableStringBuilder