【问题标题】:Change Font size programmatically in Android在 Android 中以编程方式更改字体大小
【发布时间】:2013-01-09 04:06:27
【问题描述】:

您好,我是安卓新手。我只是想知道是否有任何方法可以更改android中String的字体大小?

String name = db.getName(Id)
String str = "Name : " + name;

我希望“名称”的字体大小大于“名称”中的值。其中“名称”是我从数据库中获取的值。

请建议任何方法来做到这一点!提前致谢!!

【问题讨论】:

  • 你用什么来显示字符串? HTML? Android TextView?
  • @Thilo : 一个简单的列表视图
  • 我认为你想显示文本视图,如果是这样,请尝试使用自定义适配器并根据需要提供 android:textSize

标签: android string font-size


【解决方案1】:

使用

TextView textView = (TextView) findViewById(R.id.textView);
Spannable span = new SpannableString(str);
span.setSpan(new RelativeSizeSpan(0.8f), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(span);

在 TextView 上获取不同的 TextSizes。

【讨论】:

  • 你能解释一下你的答案吗?
【解决方案2】:

当您将文本放入 textView 时,您可以增加字体。例如

textView.setText(yourString);
textView.setTextSize(20);

或者您可以在 Layout.xml 文件本身中给出字体大小

<TextView
      android:id = "@+id/textView"
       ........
       android:textSize = "20dp"/>

如果您需要进一步说明,请随时提出任何疑问。

【讨论】:

    【解决方案3】:

    您不会更改String 的字体大小,而是在显示String 时更改文本的字体大小(例如,如果您使用的是TextView)。 String 只是一个数据对象,包含您要显示的文本,与您显示它的方式无关。

    【讨论】:

      【解决方案4】:

      您需要使用 Spannable 并将其提供给您的 TextView 以便仅修改部分文本。要更改大小,请使用:

       span.setSpan(new RelativeSizeSpan(0.8f), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
      

      【讨论】:

      • 感谢您的回复,如果我想在列表视图中显示字符串怎么办
      • 您可以做同样的事情,但是您必须创建一个自定义类来实现您需要的适配器(即:ArrayAdapter 或 BaseAdapter)并覆盖 getView() 方法。
      • Stephane Mathis:k 非常感谢 :) 你的意思是我需要创建一个自定义列表视图对吗?
      • 不,只是提供给您的列表视图的自定义适配器。看看这个例子:http://www.ezzylearning.com/tutorial.aspx?tid=1763429 和 WeatherAdapter.java。
      【解决方案5】:

      您无法更改String 的字体大小,因为String 只是一组字符,但您可以使用@987654321 更改包含此StringTextView 的字体大小@ 方法。希望这会有所帮助。

      【讨论】:

        【解决方案6】:

        你不能。因为你不能用任何其他语言。您需要做的是更改将显示文本的元素的字体大小,而不是字符串本身。

        尝试在res/layout/ 文件夹中创建一个布局,添加一个TextView 元素,然后搜索textSize 属性。

        【讨论】:

          【解决方案7】:

          在 ListView 中显示差异的最佳方法是显示标题。一个简单的例子解释了here 它声明了以下代码:

          简单的活动 Xml

          <?xml version="1.0" encoding="utf-8"?>
          <LinearLayout
              xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">
          
              <ListView
                  android:id="@+id/add_journalentry_menuitem"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content" />
              <ListView
                  android:id="@+id/list_journal"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content" />
          </LinearLayout>
          

          列表标题

          <TextView
              xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/list_header_title"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:paddingTop="2dip"
              android:paddingBottom="2dip"
              android:paddingLeft="5dip"
              style="?android:attr/listSeparatorTextViewStyle" />
          

          列表项

          <?xml version="1.0" encoding="utf-8"?>
          <!-- list_item.xml -->
          <TextView
              xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/list_item_title"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:paddingTop="10dip"
              android:paddingBottom="10dip"
              android:paddingLeft="15dip"
              android:textAppearance="?android:attr/textAppearanceLarge"
              />
          

          主要活动

          import java.util.HashMap;
          import java.util.Map;
          import android.app.Activity;
          import android.os.Bundle;
          import android.view.View;
          import android.widget.AdapterView;
          import android.widget.ArrayAdapter;
          import android.widget.ListView;
          import android.widget.Toast;
          import android.widget.AdapterView.OnItemClickListener;
          
          public class ListSample extends Activity
              {
          
                  public final static String ITEM_TITLE = "title";
                  public final static String ITEM_CAPTION = "caption";
          
                  // SectionHeaders
                  private final static String[] days = new String[]{"Mon", "Tue", "Wed", "Thur", "Fri"};
          
                  // Section Contents
                  private final static String[] notes = new String[]{"Ate Breakfast", "Ran a Marathan ...yah really", "Slept all day"};
          
                  // MENU - ListView
                  private ListView addJournalEntryItem;
          
                  // Adapter for ListView Contents
                  private SeparatedListAdapter adapter;
          
                  // ListView Contents
                  private ListView journalListView;
          
                  public Map<String, ?> createItem(String title, String caption)
                      {
                          Map<String, String> item = new HashMap<String, String>();
                          item.put(ITEM_TITLE, title);
                          item.put(ITEM_CAPTION, caption);
                          return item;
                      }
          
                  @Override
                  public void onCreate(Bundle icicle)
                      {
                          super.onCreate(icicle);
          
                          // Sets the View Layer
                          setContentView(R.layout.main);
          
                          // Interactive Tools
                          final ArrayAdapter<String> journalEntryAdapter = new ArrayAdapter<String>(this, R.layout.add_journalentry_menuitem, new String[]{"Add Journal Entry"});
          
                          // AddJournalEntryItem
                          addJournalEntryItem = (ListView) this.findViewById(R.id.add_journalentry_menuitem);
                          addJournalEntryItem.setAdapter(journalEntryAdapter);
                          addJournalEntryItem.setOnItemClickListener(new OnItemClickListener()
                              {
                                  @Override
                                  public void onItemClick(AdapterView<?> parent, View view, int position, long duration)
                                      {
                                          String item = journalEntryAdapter.getItem(position);
                                          Toast.makeText(getApplicationContext(), item, Toast.LENGTH_SHORT).show();
                                      }
                              });
          
                          // Create the ListView Adapter
                          adapter = new SeparatedListAdapter(this);
                          ArrayAdapter<String> listadapter = new ArrayAdapter<String>(this, R.layout.list_item, notes);
          
                          // Add Sections
                          for (int i = 0; i < days.length; i++)
                              {
                                  adapter.addSection(days[i], listadapter);
                              }
          
                          // Get a reference to the ListView holder
                          journalListView = (ListView) this.findViewById(R.id.list_journal);
          
                          // Set the adapter on the ListView holder
                          journalListView.setAdapter(adapter);
          
                          // Listen for Click events
                          journalListView.setOnItemClickListener(new OnItemClickListener()
                              {
                                  @Override
                                  public void onItemClick(AdapterView<?> parent, View view, int position, long duration)
                                      {
                                          String item = (String) adapter.getItem(position);
                                          Toast.makeText(getApplicationContext(), item, Toast.LENGTH_SHORT).show();
                                      }
                              });
                      }
          
              }
          

          那里还有两个额外的 XML,但是通过这些给定的结构,您可以以更好的方式实现您想要的,而不仅仅是大小差异。

          P.S.- 完整的应用程序可用here

          【讨论】:

          • MoJo:谢谢你的帮助..我会看看它.. :)
          【解决方案8】:

          可扩展字体:为了给文本的某些部分设置不同的字体大小,可以使用RelativeSizeSpan,如下例所示:

          Spannable spannable = new SpannableString(firstWord+lastWord); 
          spannable.setSpan(new ForegroundColorSpan(firstWordColor), 0, firstWord.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
          spannable.setSpan(new ForegroundColorSpan(lastWordColor), firstWord.length(), firstWord.length()+lastWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
          textview.setText( spannable );
          

          来源:从this webpage复制和粘贴。

          【讨论】:

            【解决方案9】:

            你不能那样做。您可以将string 添加到视图中,比如说EditTextTextView,并在xml 中设置视图的文本大小,如下所示:

            android:textSize="18dp"
            

            或以编程方式,您可以这样做:

            <YourTextView>.setTextSize(18);
            

            由于之前缺乏想法,我曾问过类似的问题here。我希望你能通过这个链接得到一个想法。

            【讨论】:

            • Avadhani:感谢您的帮助。所以似乎不可能改变字符串的字体大小! :(
            • @Deepthi 为什么它看起来那么深,setTextSize() 有效。请告诉我,我会尽力提供帮助。
            • @Deepthi 是的。字符串只是一些文本的表示。应用一些样式信息取决于 GUI 框架。你明白吗?
            • Avadhani:是的,我愿意.. 但我想在列表视图中显示这个字符串
            • 然后使用自定义适配器...将字符串设置为列表视图并更改大小
            猜你喜欢
            • 2015-08-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-01-22
            • 1970-01-01
            • 2012-09-24
            • 1970-01-01
            • 2015-01-09
            相关资源
            最近更新 更多