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