在Android中以编程方式更改字体大小
嗨,我是安卓系统的新手。我只是想知道是否有任何方法可以改变android中字符串的字体大小在Android中以编程方式更改字体大小,android,string,font-size,Android,String,Font Size,嗨,我是安卓系统的新手。我只是想知道是否有任何方法可以改变android中字符串的字体大小 String name = db.getName(Id) String str = "Name : " + name; 我希望“Name”的字体大小大于“Name”中的值。其中“Name”是我从数据库中获取的值 请建议任何方法来做这件事!!提前谢谢 您不更改字符串的字体大小,而是在显示字符串时更改文本的字体大小(例如,如果您正在使用文本视图)。String只是一个数据对象,它包含您想要显示的文本,与您显
String name = db.getName(Id)
String str = "Name : " + name;
我希望“Name”的字体大小大于“Name”中的值。其中“Name”是我从数据库中获取的值
请建议任何方法来做这件事!!提前谢谢 您不更改
字符串的字体大小
,而是在显示字符串
时更改文本的字体大小(例如,如果您正在使用文本视图
)。String
只是一个数据对象,它包含您想要显示的文本,与您显示文本的方式无关,您不能更改String
的字体大小,因为String
只是一组字符,但是您可以使用该方法更改包含此字符串的TextView
的字体大小。希望这有帮助。你不能那样做。您可以将字符串
添加到视图中,例如EditText
或TextView
,并按如下方式设置xml视图的文本大小:
android:textSize="18dp"
或者通过编程,您可以这样做:
<YourTextView>.setTextSize(18);
.setTextSize(18);
由于之前缺乏想法,我曾问过类似的问题。我希望你能对这个链接有所了解。你需要使用一个Spannable并将它交给你的TextView,以便只修改文本的一部分。要更改大小,请使用:
span.setSpan(new RelativeSizeSpan(0.8f), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
你不能。因为你不能用任何其他语言。您需要做的是更改将显示文本的元素的大小,而不是字符串本身
尝试在res/layout/
文件夹中创建布局,添加TextView元素,然后搜索textSize
属性。使用
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.setText(yourString);
textView.setTextSize(20);
也可以在Layout.xml文件中指定字体大小
<TextView
android:id = "@+id/textView"
........
android:textSize = "20dp"/>
如果您需要进一步澄清,请随时询问任何进一步的疑问。在列表视图中显示差异的最佳方法是显示标题。下面解释了一个简单示例,其中说明了以下代码:
简单活动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();
}
});
}
}
import java.util.HashMap;
导入java.util.Map;
导入android.app.Activity;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
导入android.widget.Toast;
导入android.widget.AdapterView.OnItemClickListener;
公共类ListSample扩展活动
{
公共最终静态字符串项\u TITLE=“TITLE”;
公共最终静态字符串项_CAPTION=“CAPTION”;
//节标题
私有最终静态字符串[]天=新字符串[]{“周一”、“周二”、“周三”、“周四”、“周五”};
//章节内容
private final static String[]notes=new String[]{“吃过早餐”、“跑过马拉松……耶,真的”、“睡了一整天”};
//菜单-列表视图
私有ListView addJournalEntryItem;
//ListView内容的适配器
专用分离列表适配器;
//列表视图内容
私有列表视图日志视图;
公共地图createItem(字符串标题、字符串标题)
{
Map item=newhashmap();
项目名称(项目名称、标题);
项目.放置(项目标题,标题);
退货项目;
}
@凌驾
创建公共空间(捆绑冰柱)
{
超级冰柱;
//设置视图图层
setContentView(R.layout.main);
//交互式工具
final ArrayAdapter journalEntryAdapter=new ArrayAdapter(这个,R.layout.add_journalentry_menuitem,新字符串[]{“add Journal Entry”});
//AddJournalEntryItem
addJournalEntryItem=(ListView)this.findViewById(R.id.add\u journalentry\u菜单项);
addJournalEntryItem.setAdapter(journalEntryAdapter);
addJournalEntryItem.setOnItemClickListener(新的OnItemClickListener()
{
@凌驾
public void onItemClick(AdapterView父视图、视图视图、整型位置、长持续时间)
{
String item=journalEntryAdapter.getItem(位置);
Toast.makeText(getApplicationContext(),item,Toast.LENGTH_SHORT.show();
}
});
//创建ListView适配器
适配器=新的分离列表适配器(此);
ArrayAdapter listadapter=新的ArrayAdapter(此,R.layout.list_项,注释);
//添加节
对于(int i=0;i
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 );