【问题标题】:How can I have a character separating line in TextView如何在 TextView 中设置字符分隔线
【发布时间】:2016-11-17 03:48:39
【问题描述】:

我想在TextView 中的每个字符之间有一条线。

原来的TextView可以这样吗?如果没有,我该怎么做?

【问题讨论】:

  • 你可以用 |否则需要在TextView之间添加View
  • 你需要为所有 0 使用单独的 TextView 看我的回答
  • 请接受我的回答。那么只有其他人才能轻松找到正确答案

标签: android xml android-layout android-studio textview


【解决方案1】:

你可以使用这个库

https://github.com/pinball83/Masked-Edittext

      [![enter image description here][1]][1]
     <com.github.pinball83.maskededittext.MaskedEditText
            android:id="@+id/masked_edit_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="number"
            app:mask="*|*|*|*|*"
            app:notMaskedSymbol="*"
            app:maskIcon="@drawable/abc_ic_clear_mtrl_alpha"
            app:maskIconColor="@color/colorPrimary"
            />

【讨论】:

  • 再次检查答案。
【解决方案2】:

如果您从 xml 填充 TextView,您可以执行以下操作

android:text="Line1: \n Line2 \n Line3"

你也可以用android:lines="2"指定行数

然后你可以用 \n

将它们分开

此外,您可以执行以下操作(尝试从代码更新文本时):

String newText = "Line 1"
           + System.getProperty ("line.separator")
           + "Line 2"
           + System.getProperty ("line.separator");

【讨论】:

    【解决方案3】:

    单个 textview 将无法做到这一点。所以

    试试

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    
        <TextView
            android:id="@+id/item1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:gravity="center"
            android:text="0"
            android:textSize="24sp" />
    
        <View
            android:layout_width="1dip"
            android:layout_height="40dip"
            android:background="#000000" />
    
        <TextView
            android:id="@+id/item2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:gravity="center"
            android:text="0"
            android:textSize="24sp" />
    
        <View
            android:layout_width="1dip"
            android:layout_height="40dip"
            android:background="#000000" />
    
        <TextView
            android:id="@+id/item3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:gravity="center"
            android:text="0"
            android:textSize="24sp" />
    
        <View
            android:layout_width="1dip"
            android:layout_height="40dip"
            android:background="#000000" />
    
        <TextView
            android:id="@+id/item4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:gravity="center"
            android:text="0"
            android:textSize="24sp" />
    
        <View
            android:layout_width="1dip"
            android:layout_height="40dip"
            android:background="#000000" />
    </LinearLayout >
    

    【讨论】:

    • 这不是很有帮助吗?@Sumit
    • 如果我的回答对您有所帮助,而不是接受作为回答,以便其他用户得到指导。@Sumit
    【解决方案4】:

    试试这个,

    main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:padding="3dp"
              android:orientation="horizontal"
              android:background="@drawable/outline"
              android:layout_height="wrap_content">
    
    
    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="#000000"
        android:gravity="center"
        android:text="0"
        android:textSize="20dp"/>
    
    <View android:layout_width="2dp"
        android:layout_height="match_parent"
        android:background="#353535"/>
    
    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="#000000"
        android:text="0"
        android:gravity="center"
        android:textSize="20dp"/>
    
    <View android:layout_width="2dp"
        android:layout_height="match_parent"
        android:background="#353535"/>
    
    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="#000000"
        android:gravity="center"
        android:text="0"
        android:textSize="20dp"/>
    
    <View android:layout_width="2dp"
        android:layout_height="match_parent"
        android:background="#353535"/>
    
    
    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="#000000"
        android:gravity="center"
        android:text="0"
        android:textSize="20dp"/>
    
    <View android:layout_width="2dp"
        android:layout_height="match_parent"
        android:background="#353535"/>
    
    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:textColor="#000000"
        android:text="0"
        android:textSize="20dp"/>
    
      </LinearLayout>
    

    outline.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"  android:id="@+id/listview_background_shape">
    <stroke android:width="2dp" android:color="#353535" />
    <padding android:left="2dp"
        android:top="2dp"
        android:right="2dp"
        android:bottom="2dp" />
    <corners android:radius="0dp" />
    <solid android:color="#ffffffff" />
    </shape>
    

    截图

    这可能对你有帮助

    【讨论】:

    • 请接受我的回答。那么只有其他人才能轻松找到正确答案
    猜你喜欢
    • 2016-09-22
    • 1970-01-01
    • 1970-01-01
    • 2013-10-10
    • 1970-01-01
    • 1970-01-01
    • 2011-07-16
    • 2016-01-16
    • 1970-01-01
    相关资源
    最近更新 更多