以特定方式对齐 TextView
Align TextViews in specific way
我目前正在 Android 中学习一些技术,我想通过按下按钮(通过 Java)将 TextView 添加到应用程序(视图)中。但在通过代码添加它们之前,我想为每个 TextView
添加一个来自 "res/values/styles.xml" 的自制预配置样式。这是目前的主要想法。
基本上我想知道如何在 XML 文件的 XML 文件中配置我的 TextView(在 styles.xml 中)和给定的布局(例如 main_activity.xml) ] 以这样的方式让他们看起来像这张照片:
所以我的目标是以这样一种方式预配置 Layout 和 TextView,我只需要一个接一个地添加 TextView,这样它们就可以按照图片中的方式对齐。
为了实现这个目标,我必须做什么?
// main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
...>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView style="@style/AddedTextView" />
</RelativeLayout>
...
<Button
android:id="@+id/btn_add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
// res/values/styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AddedTextView" parent="Widget.AppCompat.TextView">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:background">@drawable/custom_textview_design</item>
<item name="android:text">TestTV</item>
<item name="android:textColor">@color/common_text_color</item>
<item name="android:textSize">7pt</item>
<item name="android:layout_alignParentLeft">true</item>
</style>
</resources>
您可以使用 ContextThemeWrapper
:
以编程方式应用样式
val textView = TextView(ContextThemeWrapper(this, R.style.AddedTextView))
并且要将 TextView
相互连接并垂直包裹,您可以使用 Google 的 FlexBoxLayout
基本布局如下:
<com.google.android.flexbox.FlexboxLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/flexLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:flexDirection="row"
app:flexWrap="wrap">
</com.google.android.flexbox.FlexboxLayout>
以编程方式添加视图非常简单:
val flexboxLayout = findViewById<View>(R.id.flexLayout) as FlexboxLayout
for (i in 1..50) {
val textView = TextView(ContextThemeWrapper(this, R.style.AddedTextView))
val layoutParams = FrameLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT)
layoutParams.rightMargin = 40
textView.layoutParams = layoutParams
textView.text = "TextView $i"
flexboxLayout.addView(textView)
}
我目前正在 Android 中学习一些技术,我想通过按下按钮(通过 Java)将 TextView 添加到应用程序(视图)中。但在通过代码添加它们之前,我想为每个 TextView
添加一个来自 "res/values/styles.xml" 的自制预配置样式。这是目前的主要想法。
基本上我想知道如何在 XML 文件的 XML 文件中配置我的 TextView(在 styles.xml 中)和给定的布局(例如 main_activity.xml) ] 以这样的方式让他们看起来像这张照片:
所以我的目标是以这样一种方式预配置 Layout 和 TextView,我只需要一个接一个地添加 TextView,这样它们就可以按照图片中的方式对齐。
为了实现这个目标,我必须做什么?
// main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
...>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView style="@style/AddedTextView" />
</RelativeLayout>
...
<Button
android:id="@+id/btn_add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
// res/values/styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AddedTextView" parent="Widget.AppCompat.TextView">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:background">@drawable/custom_textview_design</item>
<item name="android:text">TestTV</item>
<item name="android:textColor">@color/common_text_color</item>
<item name="android:textSize">7pt</item>
<item name="android:layout_alignParentLeft">true</item>
</style>
</resources>
您可以使用 ContextThemeWrapper
:
val textView = TextView(ContextThemeWrapper(this, R.style.AddedTextView))
并且要将 TextView
相互连接并垂直包裹,您可以使用 Google 的 FlexBoxLayout
基本布局如下:
<com.google.android.flexbox.FlexboxLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/flexLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:flexDirection="row"
app:flexWrap="wrap">
</com.google.android.flexbox.FlexboxLayout>
以编程方式添加视图非常简单:
val flexboxLayout = findViewById<View>(R.id.flexLayout) as FlexboxLayout
for (i in 1..50) {
val textView = TextView(ContextThemeWrapper(this, R.style.AddedTextView))
val layoutParams = FrameLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT)
layoutParams.rightMargin = 40
textView.layoutParams = layoutParams
textView.text = "TextView $i"
flexboxLayout.addView(textView)
}