【问题标题】:How to add vertical lines to the left of my text?如何在我的文本左侧添加垂直线?
【发布时间】:2016-08-11 12:19:29
【问题描述】:

如何在文本左侧添加垂直线?
我在 TextView 中动态显示块引用 html。

【问题讨论】:

  • 您也可以使用 compound drawable(在本例中为drawableLeft),并节省额外的视图。
  • 你能解释更多吗?
  • 请用谷歌搜索android textview compound drawable

标签: android html textview blockquote


【解决方案1】:

放置在您的textView 左侧并使用您想要的尺寸和颜色。

     <View
        android:layout_width="2dp"
        android:layout_height="100dp"
        android:background="@color/colorPrimaryDark"></View>

【讨论】:

  • 想动态创建textview,从html设置文本。
【解决方案2】:

你可以使用View:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:padding="10dp"
  android:orientation="horizontal">

  <View   
    android:layout_gravity="right"
    android:background="#000000"
    android:layout_width="2dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_height="match_parent" />

  <TextView
   android:text="balblalblablalblablalblalb"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content" />

</LinearLayout>

它看起来像:

要显示Html,您可以执行以下操作:

yourTextView.setText(Html.fromHtml("your html string"));

【讨论】:

  • 想动态创建textview,从html设置文本。
  • 您可以根据需要动态创建TextView,例如:TextView t = new TextView(context);,而不是设置LayoutParams
【解决方案3】:

使用

    <View android:id="@+id/view"
          android:layout_height="match_parent"
          android:layout_width="1dp"
          android:background="#000000" />

对 HTML 文本使用 webview

    WebView webview = (WebView)this.findViewById(R.id.webview);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.loadDataWithBaseURL("", data, "text/html", "UTF-8", "");

这是你想要的代码

      String a = "You can ";
      String b = "change" ; 
      String c = "like this" ; 
      String d = "if want to make it bold"; 
      String sourceString = a + " " + "<b>" + b + "</b>" + " " + c + " " + "<b>" + d + "</b>"; 
      textView.setText(Html.fromHtml(sourceString));

看起来像

你可以像这样改变如果想加粗

【讨论】:

  • 想动态创建textview,从html设置文本。
  • 不使用webview
  • @BincyBaby 您可以添加textview 并将文本设置为.setText(Html.fromHtml("Your html string"))
  • 您到底想做什么。请解释@BincyBaby
【解决方案4】:
Step 1: Add a Linear layout in a layout.
<LinearLayout
    android:id="@+id/linear_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_margin="10dp">

</LinearLayout> 
step2:In Activity , create TextView and a View dynamically  add to linear layout
 LinearLayout mylayout = (LinearLayout) findViewById(R.id.linear_layout);

    TextView text = new TextView(this);
    text.setText("Testing");

    View view = new View(this);
    view.setBackgroundColor(getResources().getColor(R.color.black));
    view.setLayoutParams(new LinearLayout.LayoutParams(5, LinearLayout.LayoutParams.MATCH_PARENT));

    mylayout.addView(view,0);
    mylayout.addView(text,1);

希望对您有所帮助!

【讨论】:

    猜你喜欢
    • 2015-11-25
    • 2021-05-19
    • 2019-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-08
    • 1970-01-01
    相关资源
    最近更新 更多