简体   繁体   English

Android textview-可以为每个字母加上边框(例如表格边框)

[英]Android textview - possible to border each letter (like a table border)

I want to know if its possible to design my textview in a way where 8000 LP would look like [8|0|0|0] LP (with the top and bottom border as well). 我想知道是否有可能以8000 LP看起来像[8|0|0|0] LP (以及顶部和底部边框)的方式设计textview。 I tried looking it up but all I could find is how people want an outline/shadow border on text. 我尝试查找它,但是我只能找到人们想要文本上的轮廓/阴影边框的方式。 I dont want to create a table in my layout if thats possible, but if its required please give an example, tailored to my code format. 如果可以的话,我不想在自己的布局中创建表格,但是如果需要的话,请举一个适合我的代码格式的示例。

Heres an example of what i mean... it has each number seperated by that square border. 这是我的意思的示例...每个数字都由那个方形边框分隔。 决斗链接

Heres my xml(shortened for relevant info only). 这是我的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"/>

Current Layout 当前布局

布局

The code provided will lead to a view as shown in the picture. 提供的代码将导致如图所示的视图。 If it is what you want you can follow along or you can customize it to what you want! 如果您想要的是您可以遵循的,也可以根据自己的需要自定义它!

提供的代码将导致如图所示的视图。如果您想要的是您可以遵循的,或者可以根据需要定制它:

First you need to make a bordering xml drawable . 首先,您需要制作一个边框xml drawable This will be used as a background to each part of your text. 这将用作文本各部分的背景。 And around each letter or number in your text. 以及文字中的每个字母或数字。 Make a resource file inside drawable folder lets call it border.xml (full name res/drawable/border.xml ).Copy and paste this inside it: 在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>

This will bring a bordering effect on your text, You can also edit color and padding to fit your needs. 这将为您的文本带来边框效果。您还可以根据需要编辑颜色和填充。 The we need to create a layout with TextView as the root view. 我们需要创建一个以TextView作为根视图的布局。 So in your layout folder make a file lets call it border_text_view.xml (full name res/layout/border_text_view.xml ). 因此,在您的布局文件夹中创建一个文件,使其border_text_view.xml (全名res/layout/border_text_view.xml )。 Copy and Paste the code below in it: 复制并粘贴下面的代码:

    <?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"/>

Note: The background is set to the previous declared border.xml from the drawable folder. 注意:背景设置为可绘制文件夹中先前声明的border.xml

Then in your layout where you plan to display the bordering text lets say its activity_main (it can be any layout you want to display the view). 然后,在计划显示边框文本的布局中,说出其activity_main (可以是您要显示视图的任何布局)。

Add this to that layout: 将此添加到该布局:

    .......
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/layout_border"/>
    .......

Then in your layout's Java Class(Activity or Fragment) get a reference to the Linear layout added above and call the method as follows: 然后在布局的Java类(活动性或片段)中获取对上面添加的线性布局的引用,并按如下所示调用方法:

    LinearLayout linearLayout=(LinearLayout)findViewById(R.id.layout_border);  
    addBorderedView(this, linearLayout, "8000LP"); 

Now make the method addBorderedView as follows: 现在,使方法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);
    }
}

To improve performance you may need to make a view holder if you plan to display large sentences this way, if you are not then you are good to go! 为了提高性能,如果您打算以这种方式显示较大的句子,则可能需要使用视图持有者;如果您不打算这样做,则可以使用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM