Android在textview

时间:2015-04-29 04:02:51

标签: android

我正在创建一个应用程序,其中包含一些具有步骤的描述。例如:

如何转让:

  • - 你有信用卡吗

  • - 如果你不做假的

  • - 然后将其插入ATM

  • - 并观看它爆炸

如何捕鱼:

  • 打开控制台
  • 使用作弊
  • 你有一个

越来越长......

这些描述(步骤)将以滚动视图显示,现在我使用java,setText使用" \ n"和" "额外的空白空间来欺骗diplay。然而,这不适用于不同的分辨率。如何使textview显示这样的东西?

根据答案,我试过这个: 布局上的.xml

 <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="16dp"
        android:layout_marginTop="16dp" >
            <TextView
                android:id="@+id/tvCBAjud"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"          
                android:text="@string/transfer"
                android:textSize="16sp"
                />
    </ScrollView>

并定义字符串:

 <string name="transfer">how to transfer \n
        <ul>
        <li>-Do you have credit card</li> 
        <li>-If you dont make a fake one</li>
         <li>-Then insert it to ATM</li> 
         <li>-And watch it explode</li>
         </ul></string>

但输出变为:

How to transfer -Do you have credit card -If you dont make a fake one -Then insert it to ATM -And watch it explode

6 个答案:

答案 0 :(得分:8)

如果您的文字是静态的,那么您可以将TextView扩展为在BulletSpan之前自动添加android:text

<强> layout.xml

<BulletTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Orange" />
<BulletTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Banana" />

<强> BulletTextView.java

/**
 * {@link TextView} that automatically adds bullet point to text if set in layout
 */
public class BulletTextView extends TextView {
    public BulletTextView(Context context) {
        super(context);
        addBullet();
    }

    public BulletTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        addBullet();
    }

    public BulletTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        addBullet();
    }

    public BulletTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        addBullet();
    }

    private void addBullet() {
        CharSequence text = getText();
        if (TextUtils.isEmpty(text)) {
            return;
        }
        SpannableString spannable = new SpannableString(text);
        spannable.setSpan(new BulletSpan(16), 0, text.length(), 0);
        setText(spannable);
    }
}

您可以通过添加缩进的自定义属性来进一步自定义。

答案 1 :(得分:7)

您可以使用 Unicode作为项目符号

    TextView tv = (TextView) findViewById(R.id.textView1);
    String circle = "\u25CF";
    String sentence = circle + "  " + "Hello its me"+ "\n";
    tv.setText(sentence);

通过更改 Unicode 和,使用任何符号,项目符号等 Unicode表在这里。
http://unicode-table.com/en/#box-drawing

答案 2 :(得分:1)

在string.xml中编写以下代码:

<string name="transfer">how to transfer \n<ul><li>-Do you have credit card</li>
<li>-If you dont make a fake one</li>
<li>-Then insert it to ATM</li>
<li>-And watch it explode</li>
</ul></string>

 <string name="catch"><ul><li>Open console</li>
<li>use cheat</li>
<li>you got one</li>
</ul></string>

并在您的activity.xml中

   <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/transfer"       
        android:textSize="@dimen/textsize_16sp" />

 <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/catch"       
        android:textSize="@dimen/textsize_16sp" />

答案 3 :(得分:1)

您可以使用<br>代替\ n,并致电HTML.fromHtml将其转换为HTML。

像这样:

String sHTML = " <string name=\"transfer\">how to transfer<br>" +
    "<br>" +
    "&#8226; Do you have credit card<br>" +
    "&#8226; If you dont make a fake one<br>" +
    "&#8226; Then insert it to ATM<br>" +
    "&#8226; And watch it explode<br>" +
    "</string>";

TextView textView1 = (TextView)findViewById(R.id.tvCBAjud);
textView1.setText(Html.fromHtml(sHTML, null, null));

答案 4 :(得分:0)

您可以按照自己的方式自定义BulletSpan:

    public class CustomBulletSpan implements LeadingMarginSpan, ParcelableSpan {
    private final int mGapWidth;
    private final boolean mWantColor;
    private final int mColor;

    private static final int BULLET_RADIUS = 3;
    private static Path sBulletPath = null;
    public static final int STANDARD_GAP_WIDTH = 2;

    public CustomBulletSpan(int gapWidth) {
        mGapWidth = gapWidth;
        mWantColor = false;
        mColor = 0;
    }

    public int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(mGapWidth);
        dest.writeInt(mWantColor ? 1 : 0);
        dest.writeInt(mColor);
    }

    public int getLeadingMargin(boolean first) {
        return 2 * BULLET_RADIUS + mGapWidth;
    }

    public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int baseline, int bottom,
            CharSequence text, int start, int end, boolean first, Layout l) {
        // Here I shifted the bullets to right by the given half bullet
        x += mGapWidth / 2;
        if (((Spanned) text).getSpanStart(this) == start) {
            Paint.Style style = p.getStyle();
            int oldcolor = 0;

            if (mWantColor) {
                oldcolor = p.getColor();
                p.setColor(mColor);
            }

            p.setStyle(Paint.Style.FILL);

            if (c.isHardwareAccelerated()) {
                if (sBulletPath == null) {
                    sBulletPath = new Path();
                    // Bullet is slightly better to avoid aliasing artifacts on
                    // mdpi devices.
                    sBulletPath.addCircle(0.0f, 0.0f, 1.2f * BULLET_RADIUS, Direction.CW);
                }

                c.save();
                c.translate(x + dir * BULLET_RADIUS, (top + bottom) / 2.0f);
                c.drawPath(sBulletPath, p);
                c.restore();
            } else {
                c.drawCircle(x + dir * BULLET_RADIUS, (top + bottom) / 2.0f, BULLET_RADIUS, p);
            }

            if (mWantColor) {
                p.setColor(oldcolor);
            }

            p.setStyle(style);
        }
    }

    @Override
    public int getSpanTypeId() {
        return 0;
    }

}

答案 5 :(得分:0)

只需添加新行\ n

    <li>-Do you have credit card</li> \n 
    <li>-If you dont make a fake one</li> \n
     <li>-Then insert it to ATM</li> \n 
     <li>-And watch it explode</li> \n

    </string>