TextView获取id代码设置字体大小颜色
时间: 2023-09-12 18:12:10 浏览: 64
您可以使用以下代码来获取TextView的id并设置字体大小和颜色:
```
TextView textView = findViewById(R.id.textViewId);
textView.setTextSize(16); // 设置字体大小为16sp
textView.setTextColor(Color.RED); // 设置字体颜色为红色
```
其中,`textViewId`应该替换为您的TextView的实际id。您可以根据需要更改字体大小和颜色。
相关问题
android studio textview字体渐变色怎么设置
要在TextView中设置字体渐变色,您可以使用TextView的getPaint()方法获取一个Paint对象,然后使用Paint对象的setShader()方法设置一个LinearGradient对象。以下是一个简单的示例代码:
```java
TextView textView = findViewById(R.id.text_view);
int startColor = Color.parseColor("#FF0000"); // 开始颜色为红色
int endColor = Color.parseColor("#0000FF"); // 结束颜色为蓝色
float startX = 0; // 渐变起点 x 坐标
float startY = 0; // 渐变起点 y 坐标
float endX = textView.getWidth(); // 渐变终点 x 坐标
float endY = textView.getHeight(); // 渐变终点 y 坐标
Shader shader = new LinearGradient(startX, startY, endX, endY, startColor, endColor, Shader.TileMode.CLAMP);
Paint paint = textView.getPaint();
paint.setShader(shader);
```
这将在TextView中创建一个从红色到蓝色的线性渐变色效果。您可以根据需要调整起点和终点的坐标来改变渐变的方向和大小。希望这可以帮助您!
如何使用kotlin设置alertdialog标题和内容的字体大小,以及按钮的字体颜色
你可以通过以下方式来设置AlertDialog标题和内容的字体大小以及按钮的字体颜色:
1. 设置标题和内容的字体大小
```kotlin
val dialog = AlertDialog.Builder(context).create()
dialog.setTitle("Title")
dialog.setMessage("Message")
// 设置标题和内容的字体大小
val titleTextView = dialog.findViewById<TextView>(android.R.id.alertTitle)
titleTextView?.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18f)
val messageTextView = dialog.findViewById<TextView>(android.R.id.message)
messageTextView?.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f)
dialog.show()
```
在上面的代码中,我们获取了AlertDialog的标题和内容的TextView,并使用setTextSize()方法来设置它们的字体大小。
2. 设置按钮的字体颜色
```kotlin
val dialog = AlertDialog.Builder(context).create()
dialog.setTitle("Title")
dialog.setMessage("Message")
// 设置按钮的字体颜色
dialog.setOnShowListener {
val positiveButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE)
positiveButton.setTextColor(ContextCompat.getColor(context, R.color.colorPrimary))
val negativeButton = dialog.getButton(DialogInterface.BUTTON_NEGATIVE)
negativeButton.setTextColor(ContextCompat.getColor(context, R.color.colorAccent))
}
dialog.show()
```
在上面的代码中,我们在AlertDialog显示之后,获取了PositiveButton和NegativeButton的Button对象,并使用setTextColor()方法来设置它们的字体颜色。我们使用ContextCompat.getColor()方法来获取颜色资源的值。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)