Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 仅以文本视图的单空格字母宽度的步长居中_Android_Textview_Center_Monospace - Fatal编程技术网

Android 仅以文本视图的单空格字母宽度的步长居中

Android 仅以文本视图的单空格字母宽度的步长居中,android,textview,center,monospace,Android,Textview,Center,Monospace,我正试图创造一个假液晶显示器 它应该存在一些普通文本,显示一些数据和背景,假装LCD外观: 我编写了这个xml,将3个文本视图相互对齐 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/footer_linearLayout_footer_parent"

我正试图创造一个假液晶显示器

它应该存在一些普通文本,显示一些数据和背景,假装LCD外观:

我编写了这个xml,将3个文本视图相互对齐

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/footer_linearLayout_footer_parent"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/dashboard_bottom_background"
    android:gravity="center"
    android:padding="3dip" >

    <TextView
        android:id="@+id/textView_heading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:ellipsize="none"
        android:lines="1"
        android:text="text"
        android:textColor="@color/holo_blue_light"
        android:textSize="28dip" />

    <TextView
        android:id="@+id/textView_heading2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:alpha="0.15"
        android:ellipsize="none"
        android:gravity="center"
        android:lines="1"
        android:text="88888888888888888888888888888888888"
        android:textColor="@color/holo_blue_light"
        android:textSize="28dip" />

    <TextView
        android:id="@+id/textView_heading3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:alpha="0.15"
        android:gravity="center"
        android:lines="1"
        android:text="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
        android:textColor="@color/holo_blue_light"
        android:textSize="28dip" />

</RelativeLayout>

在某些情况下可以正常工作,但现在看看如果最顶端视图的文本长度发生变化会发生什么:

视图不再正确对齐,因为只有上部视图更改了其宽度,因此再次居中


我想问的是,是否有可能将文本视图居中,但只能以所用的
单空间
-font

字体的一个字母宽度为单位,我认为最好的解决方案是对每个字符使用
文本视图
,每个字符都有一个可绘制的,表示两层“
8
”和“
X
”作为背景

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textView_heading"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/char_0"
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="0"
        android:textColor="@color/holo_blue_light"
        android:textSize="28dip" 
        android:background="@drawable/lcd_char"/>

    <TextView
        android:id="@+id/char_1"
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="1"
        android:textColor="@color/holo_blue_light"
        android:textSize="28dip" 
        android:background="@drawable/lcd_char"/>

    <!-- ... -->

    <TextView
        android:id="@+id/char_n"
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="n"
        android:textColor="@color/holo_blue_light"
        android:textSize="28dip" 
        android:background="@drawable/lcd_char"/>
</LinearLayout>
因此,在onCreate()方法中,可以通过以下方式检索所有led字符:

TextView[] lcdChars = new TextView[3/*lcd size*/];
for (int i = 0; i < lcdChars.length; i++) {
    int resID = getResources().getIdentifier("char_"+i, "id", getPackageName());
    lcdChars[i] = (TextView) findViewById(resID);
}
TextView[]lcdChars=新的TextView[3/*lcd size*/];
对于(int i=0;i
然后使用如下方法设置所需的文本:

private void setLCDText(char[] chars){
    for (int i = 0; i < lcdChars.length; i++) {
        if(i < chars.length){
            lcdChars[i].setText(chars, i, 1);
        }else{
            lcdChars[i].setText("");
        }
    }
}
private void setLCDText(char[]chars){
对于(int i=0;i
我认为最好的解决方案是为每个字符使用
文本视图
,每个字符都有一个可绘制的,表示两层“
8
”和“
X
”作为背景

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textView_heading"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/char_0"
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="0"
        android:textColor="@color/holo_blue_light"
        android:textSize="28dip" 
        android:background="@drawable/lcd_char"/>

    <TextView
        android:id="@+id/char_1"
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="1"
        android:textColor="@color/holo_blue_light"
        android:textSize="28dip" 
        android:background="@drawable/lcd_char"/>

    <!-- ... -->

    <TextView
        android:id="@+id/char_n"
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="n"
        android:textColor="@color/holo_blue_light"
        android:textSize="28dip" 
        android:background="@drawable/lcd_char"/>
</LinearLayout>
因此,在onCreate()方法中,可以通过以下方式检索所有led字符:

TextView[] lcdChars = new TextView[3/*lcd size*/];
for (int i = 0; i < lcdChars.length; i++) {
    int resID = getResources().getIdentifier("char_"+i, "id", getPackageName());
    lcdChars[i] = (TextView) findViewById(resID);
}
TextView[]lcdChars=新的TextView[3/*lcd size*/];
对于(int i=0;i
然后使用如下方法设置所需的文本:

private void setLCDText(char[] chars){
    for (int i = 0; i < lcdChars.length; i++) {
        if(i < chars.length){
            lcdChars[i].setText(chars, i, 1);
        }else{
            lcdChars[i].setText("");
        }
    }
}
private void setLCDText(char[]chars){
对于(int i=0;i
感谢您的输入。但是,使用该代码时,文本具有左对齐。但是,以中心为中心并没有什么大不了的。这种解决方案的缺点是,位数不再是可变的,不能依赖于屏幕大小。如果我能使用它,我会研究它,也许会做一些修改。@walla:如果你愿意,你可以在运行时根据屏幕大小将
TextView
s添加到
LinearLayout
。另一个解决方案是根据Android的需要,使用不同位数的XML布局:例如,您可以使用7英寸平板电脑使用5位数的
res\layout-sw600dp\lcd\u layout.XML
和10英寸平板电脑使用8位数的
res\layout-sw720dp\lcd\u layout.XML
。谢谢您的输入。但是,使用该代码时,文本具有左对齐。但是,以中心为中心并没有什么大不了的。这种解决方案的缺点是,位数不再是可变的,不能依赖于屏幕大小。如果我能使用它,我会研究它,也许会做一些修改。@walla:如果你愿意,你可以在运行时根据屏幕大小将
TextView
s添加到
LinearLayout
。另一个解决方案是根据Android的需要,使用不同位数的XML布局:例如,您可以使用7英寸平板电脑使用5位数的
res\layout-sw600dp\lcd\u layout.XML
,10英寸平板电脑使用8位数的
res\layout-sw720dp\lcd\u layout.XML