Java Android:styles.xml中指定的填充是否未显示在以编程方式添加的TextView中?
根据Java Android:styles.xml中指定的填充是否未显示在以编程方式添加的TextView中?,java,android,kotlin,Java,Android,Kotlin,根据styles.xml中的配置,我正在以编程方式将TextView添加到我的应用程序的UI中,并使其具有较小的填充。但是,当我运行我的应用程序时,TextView没有填充: 这似乎很奇怪,因为我相信我已经在style.xml配置中定义了填充,并在MainActivity.java中以编程方式正确设置了填充。下面是我的代码: styles.xml: <resources> <!-- Base application theme. --> <styl
styles.xml
中的配置,我正在以编程方式将TextView
添加到我的应用程序的UI中,并使其具有较小的填充。但是,当我运行我的应用程序时,TextView
没有填充:
这似乎很奇怪,因为我相信我已经在style.xml
配置中定义了填充,并在MainActivity.java
中以编程方式正确设置了填充。下面是我的代码:
styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<!-- Message alerting the user that the app has not finished scraping the schedule -->
<style name="NotDoneScrapingMsg" parent="@android:style/Widget.TextView">
<item name="android:textSize">16sp</item>
<item name="android:textColor">#000000</item>
<item name="android:paddingLeft">7dp</item>
</style>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<LinearLayout
android:id="@+id/baseLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.addtextviewwithpadding
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.widget.TextViewCompat
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val baseLayout: LinearLayout = findViewById<LinearLayout>(R.id.baseLayout)
// Create and add a TextView telling the user that the app is still retrieving the schedule
val notDoneScrapingMsg = TextView(applicationContext)
notDoneScrapingMsg.text = "Test Padding"
//valueTV.id = 5
notDoneScrapingMsg.layoutParams = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
)
TextViewCompat.setTextAppearance(notDoneScrapingMsg, R.style.NotDoneScrapingMsg)
baseLayout.addView(notDoneScrapingMsg)
}
}
@颜色/原色
@颜色/原色暗
@颜色/颜色重音
16便士
#000000
7dp
活动\u main.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<!-- Message alerting the user that the app has not finished scraping the schedule -->
<style name="NotDoneScrapingMsg" parent="@android:style/Widget.TextView">
<item name="android:textSize">16sp</item>
<item name="android:textColor">#000000</item>
<item name="android:paddingLeft">7dp</item>
</style>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<LinearLayout
android:id="@+id/baseLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.addtextviewwithpadding
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.widget.TextViewCompat
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val baseLayout: LinearLayout = findViewById<LinearLayout>(R.id.baseLayout)
// Create and add a TextView telling the user that the app is still retrieving the schedule
val notDoneScrapingMsg = TextView(applicationContext)
notDoneScrapingMsg.text = "Test Padding"
//valueTV.id = 5
notDoneScrapingMsg.layoutParams = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
)
TextViewCompat.setTextAppearance(notDoneScrapingMsg, R.style.NotDoneScrapingMsg)
baseLayout.addView(notDoneScrapingMsg)
}
}
MainActivity.kt:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<!-- Message alerting the user that the app has not finished scraping the schedule -->
<style name="NotDoneScrapingMsg" parent="@android:style/Widget.TextView">
<item name="android:textSize">16sp</item>
<item name="android:textColor">#000000</item>
<item name="android:paddingLeft">7dp</item>
</style>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<LinearLayout
android:id="@+id/baseLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.addtextviewwithpadding
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.widget.TextViewCompat
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val baseLayout: LinearLayout = findViewById<LinearLayout>(R.id.baseLayout)
// Create and add a TextView telling the user that the app is still retrieving the schedule
val notDoneScrapingMsg = TextView(applicationContext)
notDoneScrapingMsg.text = "Test Padding"
//valueTV.id = 5
notDoneScrapingMsg.layoutParams = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
)
TextViewCompat.setTextAppearance(notDoneScrapingMsg, R.style.NotDoneScrapingMsg)
baseLayout.addView(notDoneScrapingMsg)
}
}
package com.example.addtextviewwithpadding
导入androidx.appcompat.app.appcompat活动
导入android.os.Bundle
导入android.widget.LinearLayout
导入android.widget.TextView
导入androidx.core.widget.TextViewCompat
类MainActivity:AppCompatActivity(){
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val baseLayout:LinearLayout=findViewById(R.id.baseLayout)
//创建并添加一个TextView,告知用户应用程序仍在检索计划
val notDoneScrapingMsg=TextView(applicationContext)
notDoneScrapingMsg.text=“测试填充”
//valueTV.id=5
NotDoneStrapingMsg.layoutParams=LinearLayout.layoutParams(
LinearLayout.LayoutParams.MATCH_父级,
LinearLayout.LayoutParams.WRAP_内容
)
TextViewCompat.setTextAppearance(NotDoneStrapingMsg,R.style.NotDoneStrapingMsg)
baseLayout.addView(notDoneScrapingMsg)
}
}
尝试以下代码
val notDoneScrapingMsg = TextView(applicationContext)
notDoneScrapingMsg.setTextAppearance(R.style.NotDoneScrapingMsg)
on上的settextearance()
支持设置一些属性,填充不是其中之一。看
要以编程方式应用样式,可以使用
ContextThemeWrapper
包装上下文。还要注意,您应该使用活动上下文,而不是应用程序上下文。或者您可以通过编程方式设置填充。不幸的是,这不起作用。