如何在Android代码中动态加粗和下划线字符串?

5

如何在Java代码中使分行名称:分行号码:分行电话号码:分行时间:加粗和斜体?数据库数据应保持简单格式。这四个只是它们的标题。在当前情况下,它以简单格式打印/显示整个数据,用户阅读这些数据时会有问题。

String branchInfoString = "" ;

// fetching all the database data in a list
List<BranchInfo> dbDataList = _dbHandler.getAllInfo() ;

// getting that data into a string that will be displayed in TextView
for(BranchInfo aBranch : dbDataList ){

branchInfoString = branchInfoString + 

          "Branch Name: " + aBranch.getBranchName() + "\n" +
          "Branch No: " + aBranch.getBranchNo() + "\n" + 
          "Branch Phone No: " + aBranch.getBranchPhone() + "\n" + 
          "Branch Timings: " + aBranch.getBranchTiming() + "\n" + 

          "\n --------------------------------------- \n \n" ;  

}

// showing all the branch data in a text_view
setContentView(R.layout.db_branch_list) ;
TextView dbDataView = (TextView)findViewById(R.id.db_branch_list) ;
dbDataView.setText(branchInfoString) ;
5个回答

11

是的,您可以使用HTML标签来装饰TextView中的文本。这不仅包括斜体和粗体,还包括字体颜色等其他一些内容。就像这样:

branchInfoHtmlString = branchInfoString + 

      "<b>Branch Name: " + aBranch.getBranchName() + "</b>\n" +
      "<i>Branch No: " + aBranch.getBranchNo() + "</i>\n" + 
      "<font >Branch Phone No: " + aBranch.getBranchPhone() + "</font>\n" + 
      "<font color="#FFDDDDDD">Branch Timings: " + aBranch.getBranchTiming() + "</font>\n" + 

      "\n --------------------------------------- \n \n" ;  

}

// showing all the branch data in a text_view
setContentView(R.layout.db_branch_list) ;
TextView dbDataView = (TextView)findViewById(R.id.db_branch_list) ;
dbDataView.setText(Html.fromHtml(branchInfoHtmlString)) ;

并非所有的HTML标签都受支持,你可以在这里这里找到标签列表。


3
使用HTML内容来格式化您的文本,例如:
branchInfoString = branchInfoString + 

      "<b><i>Branch Name: </i></b>" + aBranch.getBranchName() + "\n" +
      "Branch No: " + aBranch.getBranchNo() + "\n" + 
      "Branch Phone No: " + aBranch.getBranchPhone() + "\n" + 
      "Branch Timings: " + aBranch.getBranchTiming() + "\n" + 

      "\n --------------------------------------- \n \n" ;

dbDataView.setText(Html.fromHtml(branchInfoString ));

1
谢谢,……它起作用了。"\n"不起作用,我想我应该在这里使用</br>标签。 - Innovator

0

你可以格式化你的字符串并将其设置在TextView中。

String some_string = Html.fromHtml(YourHTMLFromattedString)
dbDataView.setText(some_string);

0
使用 Html.fromHtml(String source) 和 HTML 标签 <b></b><i></i> 来显示一些加粗和斜体的文本。
for(BranchInfo aBranch : dbDataList ){

branchInfoString = branchInfoString + 

          "<b><i>Branch Name: </i></b>" + aBranch.getBranchName() + "\n" +
          "<b><i>Branch No: </i></b>" + aBranch.getBranchNo() + "\n" + 
          "<b><i>Branch Phone No: </i></b>" + aBranch.getBranchPhone() + "\n" + 
          "<b><i>Branch Timings: </i></b> " + aBranch.getBranchTiming() + "\n" + 

          "\n --------------------------------------- \n \n" ;  

}

//your code ...

dbDataView.setText(Html.fromHtml(branchInfoString)) ;

0
你需要创建一个 tableView,其中包含多行,在运行时会添加。第一行将包含列标题。
例如:
<TableLayout android:id="@+id/class">
    <TableRow android:id="@+id/headers">
        <TextView android:id="@+id/branch_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:typeface="serif">
        <TextView android:id="@+id/branch_no"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:typeface="serif">
        <TextView android:id="@+id/branch_phone_no"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:typeface="serif">
        <TextView android:id="@+id/branch_timings"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:typeface="serif">
    </TableRow>
</TableLayout>

要将数据添加到这个表中,在类中,您需要添加类似于以下内容的代码:

TableLayout layout =     (TableLayout)LayoutInflater.from(canvas.getContext()).inflate(R.layout.class, null);

TableRow headerRow = (TableRow) layout.findViewById(R.id.header);
TextView branchName = (TextView) layout.findViewById(R.id.branch_name);
TextView branchNo = (TextView) layout.findViewById(R.id.branch_no);
TextView branchPhoneNo = (TextView) layout.findViewById(R.id.branch_phone_no);
TextView branchTimings = (TextView) layout.findViewById(R.id.branch_timings);

branchName.setText("Branch Name");
branchName.setTypeFace(null, Typeface.BOLD);

branchNo.setText("Branch No");
branchNo.setTypeFace(null, Typeface.BOLD);

branchPhoneNo.setText("Branch Phone No");
branchPhoneNo.setTypeFace(null, Typeface.BOLD);

branchTimings.setText("Branch Timimgs");
branchTimings.setTypeFace(null, Typeface.BOLD);


for (int i=0; i<noOfRows; i++) {
    TableRow dataset = new TableRow(context);
    TextView branchName = new TextView(context);
    TextView branchNo = new TextView(context);
    TextView branchPhoneNo = new TextView(context);
    TextView branchTimings = new TextView(context);

    branchName.setText("SampleBranchName" + i);
    branchNo.setText("SampleNumber" + i);
    branchPhoneNo.setText("SamplePhoneNumber" + i);
    branchTimings.setText("SampleTimings" + i);

    dataset.addView(branchName);
    dataset.addView(branchNo);
    dataset.addView(branchPhoneNo);
    dataset.addView(branchTimings);

    layout.addView(dataset);
}

这个代码会动态地打印数值吗?我的意思是,比如说,我们在数据库中有1000个分行名称、1000个分行编号等等。这个代码会把所有数据都打印在下面吗?我尝试过只用<RelativeLayout>,但它会把每个新值都打印在前一个上面 :) - Innovator
这将为每个记录添加一个新的表行。因此,这不会覆盖先前的行 :) 这是 Android 的核心实现方式。如果这对您有帮助,请随意标记此答案为有用/正确。 - lokoko

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接