標簽:目的 ora ted find center focus 控制 單行 normal
开发工具:Android Studio
TestView(線性布局)
1、基本屬性:
layout_width:組件寬度(單位dp)
layout_height:組件高度
id:組件id
text:文本內容
textColor:字體顔色
textStyle:字體風格,normal(無效果)、bold(加粗)、itallc(斜體)
textSize:字體大小(單位sp,爲了不同手機的適配)
background:背景
gravity:內容的對其方向,TextView中是文字、ImageView中是圖片等
android:shadowRadius:陰影模糊程度(0.1爲字體顔色,一般建議3.0)
android:shadowColor:陰影顔色
android:shadowDx:陰影在水平方向的偏移,橫坐標
android:shadowDy:陰影在豎直方向的偏移,縱坐標
code:
activity_main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical"> 6 7 <TextView 8 android:id="@+id/tv_one" 9 10 android:layout_width="200dp" 11 android:layout_height="200dp" 12 13 android:text="@string/tv_on" 14 android:textColor="@color/red" 15 android:textStyle="bold" 16 android:textSize="30sp" 17 18 android:shadowColor="@color/colorPrimary" 19 android:shadowRadius="3.0" 20 android:shadowDx="10.0" 21 android:shadowDy="10.0" 22 23 android:background="@color/colorAccent" 24 25 android:layout_gravity="center_vertical" 26 27 /> 28 29 </LinearLayout>
MainActivity.java
1 package com.example.myapplicationnew; 2 3 import androidx.appcompat.app.AppCompatActivity; 4 5 import android.os.Bundle; 6 import android.widget.TextView; 7 8 public class MainActivity extends AppCompatActivity { 9 10 @Override 11 protected void onCreate(Bundle savedInstanceState) { 12 super.onCreate(savedInstanceState); 13 setContentView(R.layout.activity_main); 14 15 //無效 16 TextView tv_one = findViewById(R.id.tv_one); 17 tv_one.setText("world"); 18 } 19 }
string.xml
1 <resources> 2 <string name="app_name">My Application New</string> 3 <string name="tv_on">hellow</string> 4 </resources>
colors.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <resources> 3 <color name="colorPrimary">#6200EE</color> 4 <color name="colorPrimaryDark">#3700B3</color> 5 <color name="colorAccent">#03DAC5</color> 6 <color name="red">#FFFF0000</color> 7 </resources>
效果
2、實現跑馬燈效果的TextView
android:stringLine:内容單行显示
android:focusable:是否可以獲取焦點
android:focusableInTouchMode:用于控制視圖在接觸模式下是否可以聚焦
android:ellipsize:在哪裏可以省略文本
android:marqueeRepeatLimit:字母動畫重複的次數
code
activity_main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical"> 6 7 <com.example.myapplicationnew.MyTextView 8 android:id="@+id/tv_one" 9 10 android:layout_width="match_parent" 11 android:layout_height="200dp" 12 13 android:layout_gravity="center_vertical" 14 15 android:text="@string/tv_on" 16 android:textColor="@color/red" 17 android:textStyle="bold" 18 android:textSize="30sp" 19 20 android:shadowColor="@color/colorPrimary" 21 android:shadowRadius="3.0" 22 android:shadowDx="10.0" 23 android:shadowDy="10.0" 24 25 android:singleLine="true" 26 android:ellipsize="marquee" 27 android:marqueeRepeatLimit="marquee_forever" 28 android:focusable="true" 29 android:focusableInTouchMode="true" 30 31 android:background="@color/colorAccent"> 32 33 </com.example.myapplicationnew.MyTextView> 34 35 36 </LinearLayout>
or
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical"> 6 7 <TextView 8 android:id="@+id/tv_one" 9 10 android:layout_width="match_parent" 11 android:layout_height="200dp" 12 13 android:layout_gravity="center_vertical" 14 15 android:text="@string/tv_on" 16 android:textColor="@color/red" 17 android:textStyle="bold" 18 android:textSize="30sp" 19 20 android:shadowColor="@color/colorPrimary" 21 android:shadowRadius="3.0" 22 android:shadowDx="10.0" 23 android:shadowDy="10.0" 24 25 android:singleLine="true" 26 android:ellipsize="marquee" 27 android:marqueeRepeatLimit="marquee_forever" 28 android:focusable="true" 29 android:focusableInTouchMode="true" 30 31 android:background="@color/colorAccent"> 32 33 <requestFocus/> 34 35 </TextView> 36 37 38 </LinearLayout>
MainActivity.java
1 package com.example.myapplicationnew; 2 3 import androidx.appcompat.app.AppCompatActivity; 4 5 import android.os.Bundle; 6 import android.widget.TextView; 7 8 public class MainActivity extends AppCompatActivity { 9 10 @Override 11 protected void onCreate(Bundle savedInstanceState) { 12 super.onCreate(savedInstanceState); 13 setContentView(R.layout.activity_main); 14 15 //TextView tv_one = findViewById(R.id.tv_one); 16 //tv_one.setText("world"); 17 } 18 }
MyTextView.java
1 package com.example.myapplicationnew; 2 3 import android.content.Context; 4 import android.util.AttributeSet; 5 import android.widget.TextView; 6 7 import androidx.annotation.Nullable; 8 9 //創建MyTextView,繼承TextView,目的是獲取焦點 10 public class MyTextView extends androidx.appcompat.widget.AppCompatTextView { 11 public MyTextView(Context context) { 12 super(context); 13 } 14 15 public MyTextView(Context context, @Nullable AttributeSet attrs) { 16 super(context, attrs); 17 } 18 19 public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 20 super(context, attrs, defStyleAttr); 21 } 22 23 @Override 24 //獲取焦點 25 public boolean isFocused() { 26 return true; 27 } 28 }
string.xml
1 <resources> 2 <string name="app_name">My Application New</string> 3 <string name="tv_on">01 Good evening! Good evening! Good evening! Good evening!</string> 4 </resources>
activity_main.xml再加入允許點擊,點一下才開始跑馬燈
1 android:clickable="true"
效果
標簽:目的 ora ted find center focus 控制 單行 normal
原文地址:https://www.cnblogs.com/0xiaoyu/p/14686883.html