Android开发布局写法

SetContentView:它的作用就是把我们的布局文件放在Activity中显示
在java文件中使用 即使用activity_main2.xml的布局样式。

setContentView(R.layout.activity_main2);

布局

文本显示

设置文本:

方法一:在java文件里写

方法二:在xml文件里写

字体大小颜色:

推荐单位:sp:根据系统字体大小变

方法一:在java文件里写
1.在TextViewActivity 后补充extend AppCompatActivity继承

public class TextViewActivity extends AppCompatActivity

2.写onCreate函数

public class TextViewActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_view);//使用activity_text_view.xml布局样式
        //从布局文件中获取名叫tv_hello的文本视图
        TextView tv_hello = findViewById(R.id.tv_hello);//找到id为tv_hello对应的文本 存到TextView的变量tv_hello
        tv_hello.setTextSize(30);//设置字体大小为30
        tv_hello.setTextColor(Color.GREEN);//设置字体颜色
         tv_hello.setBackgroundColor(R.color.black);//设置字体的背景颜色
    }
}

java设置颜色法2:

tv_hello.setTextColor(0Xff00ffff);//另一种设置字体颜色的方法

方法二:在xml文件里写

    <TextView
        android:id="@+id/sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/helllo" //在string.xml文件里用name为hello的文本
        android:textSize="60sp"//加单位!!
        android:textColor="#00ff00"  //加#!!
        />

xml设置颜色法二:

        android:textColor="@color/gold" //引用color.xml文件里

color.xml文件

    <color name="gold">#CD7f32</color>

string.xml文件

    <string name="helllo">你好吗世界</string>

## 视图基础

```css
match_parent //与上级视图保持一致
wrap_content //与内容自适应
    <TextView
        android:id="@+id/sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/helllo"
        android:textSize="60sp"
        android:textColor="@color/gold"
        />

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值