我可以在android中以编程方式更改字符串的字体大小吗?

时间:2013-01-25 07:58:35

标签: android string font-size

嗨,我是android的新手。我只是想知道是否有任何方法可以改变android中字符串的字体大小?

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

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

请建议任何方法来做到这一点!!在此先感谢!!

9 个答案:

答案 0 :(得分:2)

使用

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。

答案 1 :(得分:2)

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

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

或者您可以在Layout.xml文件中提供字体大小

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

如果您需要进一步澄清,请随时再提出疑问。

答案 2 :(得分:1)

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

答案 3 :(得分:1)

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

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

答案 4 :(得分:0)

您无法更改String的字体大小,因为String只是一组字符,但您可以更改包含此{的TextView的字体大小{1}}使用setTextSize()方法。希望这会有所帮助。

答案 5 :(得分:0)

你做不到。您可以将string添加到视图,例如EditTextTextView,并在xml中为视图设置文本大小,如下所示:

android:textSize="18dp"

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

<YourTextView>.setTextSize(18);

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

答案 6 :(得分:0)

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

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

答案 7 :(得分:0)

在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,但是通过这些给定的结构,您可以以更好的方式实现您想要的而不是Just size。

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

答案 8 :(得分:0)

Spannable font:为了给文本的某些部分设置不同的字体大小,可以使用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 复制并粘贴。