【发布时间】:2018-01-16 14:34:15
【问题描述】:
我想知道我是否有像“hello world”这样的文本。 如何设置此文本视图的偏好,用户可以将字体大小更改为中、大和小,如 20sp、25sp、30sp。
【问题讨论】:
-
你能告诉我你到目前为止做了什么吗?
标签: java android android-studio fonts textview
我想知道我是否有像“hello world”这样的文本。 如何设置此文本视图的偏好,用户可以将字体大小更改为中、大和小,如 20sp、25sp、30sp。
【问题讨论】:
标签: java android android-studio fonts textview
如果你有一个活动,哪个布局有一个
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World"
android:textSize="20sp"
/>
因此,您需要像按钮这样的用户输入,并且在活动中,您可以更改检测与用户输入相关的事件的 textview 元素的大小,在本例中为“onclick”方法。
这里是归档它的代码。
布局:
<?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">
<TextView
android:id="@+id/txt_hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="HelloWorld"
android:textColor="@android:color/black"
android:textSize="20sp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/txt_hello_world"
android:orientation="horizontal"
>
<Button
android:id="@+id/btn_20sp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="8dp"
android:text="20sp"
/>
<Button
android:id="@+id/btn_30sp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="8dp"
android:text="30sp"
/>
<Button
android:id="@+id/btn_40sp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="8dp"
android:text="40sp"
/>
</LinearLayout>
活动
class MainActivity : AppCompatActivity(), View.OnClickListener {
lateinit var helloText : TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
helloText = findViewById(R.id.txt_hello_world)
var btn20sp: Button = findViewById(R.id.btn_20sp)
btn20sp.setOnClickListener(this)
var btn30sp: Button = findViewById(R.id.btn_30sp)
btn30sp.setOnClickListener(this)
var btn40sp: Button = findViewById(R.id.btn_40sp)
btn40sp.setOnClickListener(this)
}
override fun onClick(view: View) {
when (view.id){
R.id.btn_20sp -> helloText.setTextSize(TypedValue.COMPLEX_UNIT_SP,20f)
R.id.btn_30sp -> helloText.setTextSize(TypedValue.COMPLEX_UNIT_SP,30f)
R.id.btn_40sp -> helloText.setTextSize(TypedValue.COMPLEX_UNIT_SP,40f)
}
}
}
代码在kotlin,但逻辑在java中是一样的
【讨论】: