小技巧
在xml中设置控件宽度为父窗口的一半
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1.0">
<Button
android:text="left"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".50"/>
<Button
android:text="right"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".50"/>
</LinearLayout>
代码
1、线性排列 LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<LinearLayout
android:id="@+id/ll_1"
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#00C1FF"
android:orientation="vertical"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:paddingBottom="20dp"
android:paddingTop="10dp"
android:layout_marginBottom="20dp">//内部元素的边距
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#9D9DF7"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#FFE65C"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:gravity="center">
<View
android:layout_width="0dp"
android:layout_height="80dp"
android:background="#FF9188"
android:layout_weight="1"/>//剩余部分权重
<View
android:layout_width="0dp"
android:layout_height="80dp"
android:background="#FFF5F5"
android:layout_weight="1"/>
<View
android:layout_width="0dp"
android:layout_height="80dp"
android:background="#C5E9AC"
android:layout_weight="1"/>
</LinearLayout>
//TextView是一个用来展示文本的控件 整个文件存放的是布局文件
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World! 这是我的第一个安卓应用"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
2、相对位置排列 RelativeLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="@+id/view_1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#FFC107" />
<View
android:id="@+id/view_2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_toRightOf="@id/view_1"
android:background="#6495ED" />
<View
android:id="@+id/view_3"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="@id/view_1"
android:background="#FF4836" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="@id/view_3"
android:background="#3F51B5"
android:orientation="horizontal"
android:padding="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#383333"
android:padding="15dp">
<View
android:id="@+id/view_6"
android:layout_width="100dp"
android:layout_height="80dp"
android:layout_weight="1"
android:background="#FFFFFF" />
<View
android:id="@+id/view_7"
android:layout_width="100dp"
android:layout_height="80dp"
android:layout_toRightOf="@id/view_6"
android:layout_weight="1"
android:background="#B1A6A6" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
3、TextView
真机运行图
代码
activity_main.xml
TextViewActivity.java
package com.hanquan.helloworld;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.Html;
import android.widget.TextView;
public class TextViewActivity extends AppCompatActivity {
//声明空间
private TextView mTv4;//中划线 消除锯齿
private TextView mTv5;//中划线 不消除锯齿
private TextView mTv6;//下划线 消除锯齿
private TextView mTv7;//使用html
private TextView mTv8;//跑马灯效果
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_view);
mTv4 = findViewById(R.id.tv_4);//找到空间
mTv4.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);//文字中划线
mTv4.getPaint().setAntiAlias(true);//消除锯齿
mTv5 = findViewById(R.id.tv_5);
mTv5.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);//文字中划线
mTv6 = findViewById(R.id.tv_6);
mTv6.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);//文字中划线
mTv6.getPaint().setAntiAlias(true);//消除锯齿
mTv7 = findViewById(R.id.tv_7);
mTv7.setText(Html.fromHtml("<u>使用html中的下划线</u>"));
mTv8 = findViewById(R.id.tv_8);
mTv8.setSelected(true);
}
}
stings.xml
MainActivity.java
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"
android:orientation="vertical"
android:padding="20dp">
<!-- 普通文字框-->
<TextView
android:id="@+id/tv_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/tv_test1"
android:textColor="#000000"
android:textSize="20sp" />
<!--超出则省略文字框-->
<!--ellipsize决定了当文字过长时,该控件该如何显示-->
<TextView
android:id="@+id/tv_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:ellipsize="end"
android:maxLines="1"
android:text="@string/tv_test1"
android:textColor="#000000"
android:textSize="20sp" />
<!--添加图片-->
<TextView
android:id="@+id/tv_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:drawableRight="@drawable/pic1"
android:drawablePadding="5dp"
android:text="筛选"
android:textColor="#3F51B5"
android:textSize="20sp" />
<!-- 文字中划线(消除锯齿)-->
<TextView
android:id="@+id/tv_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="这是一行文字,文字的中划线在java中产生,并且使用setAntiAlias(true);成功删除了锯齿"
android:textColor="#000000"
android:textSize="20sp" />
<!-- 文字中划线(未消除锯齿)-->
<TextView
android:id="@+id/tv_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="这是一行文字,文字的中划线在java中产生,并且含有锯齿"
android:textColor="#000000"
android:textSize="20sp" />
<!-- 文字下划线(消除锯齿)-->
<TextView
android:id="@+id/tv_6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="文字下划线测试"
android:textColor="#000000"
android:textSize="20sp" />
<!-- 使用html-->
<TextView
android:id="@+id/tv_7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text=""
android:textColor="#000000"
android:textSize="20sp" />
<!--ellipsize="marquee"跑马灯的效果-->
<TextView
android:id="@+id/tv_8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:ellipsize="marquee"
android:singleLine="true"
android:text="我不想被人画个圈圈成为有些人喜闻乐道的对象;我不想因为弄些子虚乌有的“新闻”被有些人熟知;我只想专注于我的工作、我的职业;我只想真诚的面对我的生活;我只想单纯的热爱我爱的一切。这条路我不急,急什么呢?争抢?欺骗?我做不到也学不会。也不希望我身边的人玩那些大家都懂的手段。也许让有的人失望了,没能成为你们想要看到的那一种星星。只想说我选择了我喜欢的职业,前进的每一步都希望坚实。懂的不必解释;不懂的,无须表达。我就是我,我还是我,我永远是我。………今天表达了很多,其实一直在心里最想感谢的是无私付出的你们~希望我们可以一直默契的,默契的走下去!!"
android:textColor="#000000"
android:textSize="20dp"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"/>
</LinearLayout>