Android textview - 可能的边框每个字母(如表格边框)

Android textview - 可能的边框每个字母(如表格边框)

问题描述:

我想知道是否有可能以8000 LP看起来像[8|0|0|0] LP(以及顶部和底部边框)的方式设计我的文本视图。我试图查找它,但我能找到的是人们想要文本上的大纲/阴影边界。如果可能的话,我不想在我的布局中创建表格,但如果需要,请给出一个示例,根据我的代码格式进行定制。Android textview - 可能的边框每个字母(如表格边框)

继承人是我的意思的一个例子...它有每个数字由该方形边界分开。 duel link

继承人我的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"/> 

当前版面

layout

+0

在字母之间放置一些字符,或将字符串更改为字符数组! – Xenolion

+0

@ Xenolion你可以发表一个如何做到这一点的例子吗? –

+0

好的。让我来做! – Xenolion

提供将导致一个视图中所示的图像中的代码。如果这是你想要的,你可以按照你的想法来定制它!

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!:

首先,你需要做一个接壤xml drawable。这将用作文本每个部分的背景。并围绕文字中的每个字母或数字。 做一个资源文件中绘制的文件夹让称它为border.xml(全名res/drawable/border.xml).Copy并粘贴在它里面:

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

注:背景设置为先前宣布脱离绘制文件夹border.xml

然后在您的布局中,您打算显示边界文字让我们说它的activity_main(它可以是任何想要显示视图的布局)。

一下添加到该布局:

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

然后在你的布局的Java类(活动或片段)得到上面添加的线性布局的参考,并调用方法如下:

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); 
    } 
} 

为了展示次数如果你打算以这种方式显示大句子,你可能需要制作一个视图持有者,如果你不是,那么你很好去!

+0

我试试看。现在,详细的解释不适合投票。如果它在我的手机ide中运行,那么它就会将其作为答案! –

+0

'addBorderedView(this,linearLayout,“8000LP”); '给我3“意外结束声明”错误 –

+0

我不知道发生了什么,但它在我的android studio项目中工作正常!尝试使用上下文ActivityName.this,就像'MainActivity.this'一样,也可以在你的活动方法中调用方法,比如'onCreate()'。同时检查括号是否组织良好。除此之外,代码工作正常,在片段中的上下文也是'getActivity()' – Xenolion