【问题标题】:How can I make bold and underline a string dynamically in Android code?如何在 Android 代码中动态加粗和下划线?
【发布时间】:2013-01-30 05:02:56
【问题描述】:

如何将分行名称:分行编号:分行电话号码:分行时间设置为粗体和斜体: 在 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) ;

【问题讨论】:

    标签: java android string


    【解决方案1】:

    是的,您可以使用 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 标签都受支持,您可以找到标签列表 herehere

    【讨论】:

      【解决方案2】:

      使用 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 ));
      

      【讨论】:

      • 谢谢,......它工作。 “\n”不起作用,我想我应该在这里使用标签
      【解决方案3】:

      你可以格式化你的字符串并在 textview 中设置它

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

      【讨论】:

        【解决方案4】:

        使用Html.fromHtml(String source) 和HTML &lt;b&gt;&lt;/b&gt;&lt;i&gt;&lt;/i&gt; 标签以粗体和斜体显示一些文本

        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)) ;
        

        【讨论】:

          【解决方案5】:

          您必须创建一个包含多行的 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-branchNames、1000-branchNo、....。这会打印另一个下方的所有数据吗?我只尝试了 但它在前一个上打印了每个新值:)
          • 这会为每条记录添加一个新的表行。所以这不会覆盖前一个 :) 这是核心的 android 方式。请随时标记此答案。如果对您有所帮助,则视为有用/正确。
          猜你喜欢
          • 1970-01-01
          • 2013-11-06
          • 2021-01-05
          • 2022-10-22
          • 2015-01-25
          • 2014-08-30
          • 2023-04-04
          • 2017-12-23
          • 1970-01-01
          相关资源
          最近更新 更多