Android textview - 可以为每个字母加边框(如 table 边框)
Android textview - possible to border each letter (like a table border)
我想知道是否有可能以 8000 LP
看起来像 [8|0|0|0] LP
的方式设计我的文本视图(也有顶部和底部边框)。我尝试查找它,但我所能找到的只是人们希望在文本上使用 outline/shadow 边框的方式。如果可能的话,我不想在我的布局中创建 table,但如果需要,请举一个适合我的代码格式的例子。
这是我的意思的一个例子...每个数字都由那个方形边框分隔。
这是我的xml(仅为相关信息而缩短)。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/default_background_obelisk"
android:scaleType="centerCrop"
android:padding="16dp">
<TextView
android:id="@+id/playerTwo_LP"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:textSize="40dp"
android:textColor="#ffffff"
android:text="8000 LP"
android:textAppearance="?android:attr/textAppearanceLarge"
android:rotation="180"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="@+id/playerTwo_addLP"
app:layout_constraintRight_toLeftOf="@+id/playerTwo_loseLP"
app:layout_constraintBottom_toBottomOf="@+id/playerTwo_toolKit"
android:textIsSelectable="true"/>
* * *
<TextView
android:id="@+id/playerOne_LP"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="8000 LP"
android:textAppearance="?android:attr/textAppearanceLarge"
app:layout_constraintTop_toTopOf="@+id/playerOne_toolKit"
app:layout_constraintLeft_toRightOf="@+id/playerOne_toolKit"
app:layout_constraintRight_toLeftOf="@+id/playerOne_CardLibrary"
app:layout_constraintBottom_toBottomOf="parent"/>
当前布局
提供的代码将导致如图所示的视图。如果它是您想要的,您可以按照您的意愿进行操作,或者您可以将其自定义为您想要的!
首先你需要做一个边框xml drawable
。这将用作文本每个部分的背景。以及文本中的每个字母或数字周围。
在 drawable 文件夹中创建一个资源文件,我们称之为 border.xml
(全名 res/drawable/border.xml
)。将其复制并粘贴到其中:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#00ffffff" />
<stroke android:width="1dip" android:color="#4fa5d5"/>
<padding android:left="4dp" android:right="4dp"
android:bottom="1dp" android:top="1dp"/>
</shape>
这将为您的文本带来边框效果,您还可以根据需要编辑颜色和填充。
我们需要创建一个以 TextView 作为根视图的布局。因此,在您的布局文件夹中创建一个文件,我们将其命名为 border_text_view.xml
(全名 res/layout/border_text_view.xml
)。复制以下代码并将其粘贴到其中:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/border"/>
注意:背景设置为之前从 drawable 文件夹中声明的 border.xml
。
然后在您计划显示边框文本的布局中说出它的 activity_main
(它可以是您想要显示视图的任何布局)。
将此添加到该布局:
.......
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/layout_border"/>
.......
然后在你的layout的Java Class(Activity or Fragment)中获取上面添加的Linear layout的引用,调用方法如下:
LinearLayout linearLayout=(LinearLayout)findViewById(R.id.layout_border);
addBorderedView(this, linearLayout, "8000LP");
现在制作方法addBorderedView
如下:
private void addBorderedView(Context context, LinearLayout layout, String string_to_display) {
String[] array = string_to_display.split("");
for (int i = 1; i < array.length; i++) {
TextView borderedTextView = (TextView) LayoutInflater.from(context).inflate(R.layout.border_text_view, null);
borderedTextView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
borderedTextView.setGravity(Gravity.CENTER);
borderedTextView.setText(array[i]);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) borderedTextView.getLayoutParams();
params.setMargins(2, 0, 2, 0); //substitute parameters for left, top, right, bottom
borderedTextView.setLayoutParams(params);
layout.addView(borderedTextView);
}
}
为了提高性能,如果您打算以这种方式显示大句子,您可能需要创建一个视图持有者,如果您不这样做,那么您就可以开始了!
我想知道是否有可能以 8000 LP
看起来像 [8|0|0|0] LP
的方式设计我的文本视图(也有顶部和底部边框)。我尝试查找它,但我所能找到的只是人们希望在文本上使用 outline/shadow 边框的方式。如果可能的话,我不想在我的布局中创建 table,但如果需要,请举一个适合我的代码格式的例子。
这是我的意思的一个例子...每个数字都由那个方形边框分隔。
这是我的xml(仅为相关信息而缩短)。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/default_background_obelisk"
android:scaleType="centerCrop"
android:padding="16dp">
<TextView
android:id="@+id/playerTwo_LP"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:textSize="40dp"
android:textColor="#ffffff"
android:text="8000 LP"
android:textAppearance="?android:attr/textAppearanceLarge"
android:rotation="180"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="@+id/playerTwo_addLP"
app:layout_constraintRight_toLeftOf="@+id/playerTwo_loseLP"
app:layout_constraintBottom_toBottomOf="@+id/playerTwo_toolKit"
android:textIsSelectable="true"/>
* * *
<TextView
android:id="@+id/playerOne_LP"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="8000 LP"
android:textAppearance="?android:attr/textAppearanceLarge"
app:layout_constraintTop_toTopOf="@+id/playerOne_toolKit"
app:layout_constraintLeft_toRightOf="@+id/playerOne_toolKit"
app:layout_constraintRight_toLeftOf="@+id/playerOne_CardLibrary"
app:layout_constraintBottom_toBottomOf="parent"/>
当前布局
提供的代码将导致如图所示的视图。如果它是您想要的,您可以按照您的意愿进行操作,或者您可以将其自定义为您想要的!
首先你需要做一个边框xml drawable
。这将用作文本每个部分的背景。以及文本中的每个字母或数字周围。
在 drawable 文件夹中创建一个资源文件,我们称之为 border.xml
(全名 res/drawable/border.xml
)。将其复制并粘贴到其中:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#00ffffff" />
<stroke android:width="1dip" android:color="#4fa5d5"/>
<padding android:left="4dp" android:right="4dp"
android:bottom="1dp" android:top="1dp"/>
</shape>
这将为您的文本带来边框效果,您还可以根据需要编辑颜色和填充。
我们需要创建一个以 TextView 作为根视图的布局。因此,在您的布局文件夹中创建一个文件,我们将其命名为 border_text_view.xml
(全名 res/layout/border_text_view.xml
)。复制以下代码并将其粘贴到其中:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/border"/>
注意:背景设置为之前从 drawable 文件夹中声明的 border.xml
。
然后在您计划显示边框文本的布局中说出它的 activity_main
(它可以是您想要显示视图的任何布局)。
将此添加到该布局:
.......
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/layout_border"/>
.......
然后在你的layout的Java Class(Activity or Fragment)中获取上面添加的Linear layout的引用,调用方法如下:
LinearLayout linearLayout=(LinearLayout)findViewById(R.id.layout_border);
addBorderedView(this, linearLayout, "8000LP");
现在制作方法addBorderedView
如下:
private void addBorderedView(Context context, LinearLayout layout, String string_to_display) {
String[] array = string_to_display.split("");
for (int i = 1; i < array.length; i++) {
TextView borderedTextView = (TextView) LayoutInflater.from(context).inflate(R.layout.border_text_view, null);
borderedTextView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
borderedTextView.setGravity(Gravity.CENTER);
borderedTextView.setText(array[i]);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) borderedTextView.getLayoutParams();
params.setMargins(2, 0, 2, 0); //substitute parameters for left, top, right, bottom
borderedTextView.setLayoutParams(params);
layout.addView(borderedTextView);
}
}
为了提高性能,如果您打算以这种方式显示大句子,您可能需要创建一个视图持有者,如果您不这样做,那么您就可以开始了!