02Android文本、视图

目录

文本显示

设置文本字体大小

设置文本的颜色

设置视图的宽高

设置视图的间距

 设置视图的对齐方式


文本显示

设置文本内容两种方式:

在XML文件中通过属性android:text设置文本

在Java代码中调用文本视图对象的setText方法设置文本

TextViewActivity.java
public class TextViewActivity extends AppCompatActivity {
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_view);
        TextView tv_hello=findViewById(R.id.tv_hello);
        tv_hello.setText("R.string.hello");
    }
}

strings.xml 

<resources>
    <string name="app_name">chapter03</string>
    <string name="hello">你好,世界</string>
</resources>

activity_text_view.xml 

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

    <TextView
        android:id="@+id/tv_hello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello"/>
</LinearLayout>

设置文本字体大小

两种方法

在Java代码中调用setTextSize方法,指定文本大小。

在XML文件中通过属性android:textSize指定文本大小,此时需要指定字号单位。

方法一:
 

<?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="@string/hello"
        android:textSize="30px"/>
</LinearLayout>

方法二:
 

package com.example.chapter03;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

public class TextSizeActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_size);
        TextView tv_hello=findViewById(R.id.tv_hello);
        tv_hello.setTextSize(30);
    }
}

px:手机屏幕的最小显示单位,与设备的显示屏有关,单个像素的大小并不固定,跟随屏幕大小和像素数量的关系变化,一个像素点为1px

dp:与设备无关的显示单位,只与屏幕的尺寸有关

sp:专门用来设置字体大小,在系统设置中可以调整字体大小

resolution分辨率:指屏幕的垂直和水平方向的像素数量

Dpi像素密度:指屏幕上每英寸(1英寸=2.54厘米)距离中有多少个像素点

Dip/dp(设备独立像素) 也可以叫做dp,长度单位,同一个单位在不同的设备上有不同的显示效果,具体效果根据设备的密度有关,公式如下。

Density:是指屏幕上每平方英寸(2.54^平方厘米)中含有的像素点数量。

 

对于相同分辨率的手机,屏幕越大,同DP的组件占用屏幕比例越小。

对于相同尺寸的手机,即使分辨率不同,同DP的组件占用屏幕比例也相同。

综上:

dp的UI效果只在相同尺寸的屏幕上相同,如果屏幕尺寸差异过大,则需要重做dp适配。

sp和dp差不多,用来专门是指字体大小,如果设置大字体,用dp设置的文字没有变化,用sp的文字变大了。

注释:ctrl+shift+/

设置文本的颜色

RGB颜色定义

 activity_text_color.xml

<?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_code_system"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="代码设置系统自带的颜色"
        android:textSize="17sp"/>
</LinearLayout>

TextColorActivity
package com.example.chapter03;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;

public class TextColorActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_color);
        //从布局文件中获取名叫tv_code_system的文本视图
        TextView tv_code_system=findViewById(R.id.tv_code_system);
        //将tv_code_system的文字颜色设置系统自带的绿色
        tv_code_system.setTextColor(Color.GREEN);
    }
}

也可以:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="purple_200">#FFBB86FC</color>
    <color name="purple_500">#FF6200EE</color>
    <color name="purple_700">#FF3700B3</color>
    <color name="teal_200">#FF03DAC5</color>
    <color name="teal_700">#FF018786</color>
    <color name="black">#FF000000</color>
    <color name="white">#FFFFFFFF</color>
    <color name="green">#00ff00</color>
</resources>

<?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_values"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="资源文件引用六位文字颜色"
        android:textSize="17sp"/>
        android:textColor="@color/green"/>
</LinearLayout>

·设置背景颜色

法一:

  <TextView
        android:id="@+id/tv_code_background"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="背景设置为绿色"
        android:textSize="17sp"
        android:background="@color/green"/>

</LinearLayout>

法二:

     //从布局文件中获取名叫tv_code_background的文本视图
        TextView tv_code_background =findViewById(R.id.tv_code_background);
        //tv_code_background.setBackgroundColor(Color.GREEN);
        //颜色来源于资源文件
        tv_code_background.setBackgroundColor(R.color.green);
<TextView
        android:id="@+id/tv_code_background"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="背景设置为绿色"
        android:textSize="17sp" />

</LinearLayout>

设置视图的宽高

android:layout_width

android:layout_height

宽高的取值主要有下列:

match_parent:表示与上级视图保持一致

wrap_content:表示与内容自适应

以dp为单位的具体尺寸

ctrl+shift+L 格式化

首先创建一个activity

package com.example.myapplication2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class ViewBorderActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_border);
    }
}
<?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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="视图宽度采用wrap_content定义"
        android:background="#00ffff"
        android:textColor="#000000"
        android:textSize="17sp"/>

</LinearLayout>

 另一种方法:用代码??

设置视图的间距

两种方式:

Layout_margin,指定了当前视图与周围平级视图之间的距离。外间距

padding,指定了当前视图与内部下级视图之间的距离。内间距

 设置视图的对齐方式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值