我无法完成它。我的应用程序从标准字体开始,自定义活动布局膨胀,然后我的字体更改。据我所知,我不能直接将自定义字体编码到我打算膨胀到操作栏的布局的xml文件中,所以我从网上的另一个主题获得了一个代码,通过引用TextView来进行更改。操作栏并在课堂上设置它。有没有办法制作字体"标准",以便应用程序已经启动显示我的自定义字体?
以下是主要活动的相关代码,我试图更改:
public class Amd extends Activity {
DatabaseHandler dh = new DatabaseHandler(this);
public ProgressDialog mProgressDialog;
public JSONObject jsonOb;
public Filme m;
public ArrayList<Genero> g;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.amd);
setActionBar();
}
@SuppressLint("InflateParams") private void setActionBar(){
this.getActionBar().setDisplayShowCustomEnabled(true);
LayoutInflater inflator = LayoutInflater.from(this);
View v = inflator.inflate(R.layout.custom_action_bar, null);
Typeface font = Typeface.createFromAsset(this.getAssets(), "tcb.ttf");
TextView textoActionBar = (TextView) getWindow().findViewById(Resources.getSystem().getIdentifier("action_bar_title", "id", "android"));
textoActionBar.setTypeface(font);
textoActionBar.setText("TESTE");
this.getActionBar().setCustomView(v);
}
这是样式xml,我也用它来设置主题并为操作栏着色:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!--
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- API 14 theme customizations can go here. -->
<item name="android:actionBarStyle">@style/PurpleActionBar</item>
</style>
<style name="PurpleActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#5D4480</item>
<item name="android:titleTextStyle">@style/ActionText</item>
</style>
<style name="ActionText" parent="@android:style/TextAppearance">
<item name="android:textColor">#975CE6</item>
</style>
</resources>
这是清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.virosys.amd"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppBaseTheme" >
<activity
android:name="com.virosys.amd.activities.Amd"
android:label="AMD"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
以下是我主要活动的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/Purple"
tools:context="com.virosys.amd.activities.Amd" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginLeft="4dp"
android:layout_marginTop="4dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="2dp"
android:onClick="listasOuFilmes"
android:scaleType="fitXY"
android:background="@null"
android:src="@drawable/filmes" />
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginRight="4dp"
android:layout_marginTop="4dp"
android:layout_marginLeft="2dp"
android:layout_marginBottom="2dp"
android:onClick="buscarFilme"
android:scaleType="fitXY"
android:background="@null"
android:src="@drawable/busca" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/ImageButton3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginLeft="4dp"
android:layout_marginBottom="4dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:onClick="inserirFilme"
android:scaleType="fitXY"
android:background="@null"
android:src="@drawable/noimage" />
<ImageButton
android:id="@+id/ImageButton4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:scaleType="fitXY"
android:background="@null"
android:src="@drawable/config" />
</LinearLayout>
</LinearLayout>
我还需要指定什么,或者您是否需要其他任何代码来了解我尝试做什么?
答案 0 :(得分:3)
由于Android ActionBar setTitle
方法收到CharSequence
作为参数,您只需将setActionBar()
方法的调用替换为下一行,即使用span:
Typeface font = Typeface.createFromAsset(this.getAssets(), "tcb.ttf");
String actionBarTitle = "TESTE";
SpannableStringBuilder ssb = new SpannableStringBuilder(actionBarTitle);
ssb.setSpan(new CustomTypefaceSpan("", font), 0, actionBarTitle.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
getActionBar().setTitle(ssb);
并且不要忘记将此自定义字体span类添加到项目中。
public class CustomTypefaceSpan extends TypefaceSpan {
private final Typeface newType;
public CustomTypefaceSpan(String family, Typeface type) {
super(family);
newType = type;
}
@Override
public void updateDrawState(TextPaint ds) {
applyCustomTypeFace(ds, newType);
}
@Override
public void updateMeasureState(TextPaint paint) {
applyCustomTypeFace(paint, newType);
}
private static void applyCustomTypeFace(Paint paint, Typeface tf) {
int oldStyle;
Typeface old = paint.getTypeface();
if (old == null) {
oldStyle = 0;
} else {
oldStyle = old.getStyle();
}
int fake = oldStyle & ~tf.getStyle();
if ((fake & Typeface.BOLD) != 0) {
paint.setFakeBoldText(true);
}
if ((fake & Typeface.ITALIC) != 0) {
paint.setTextSkewX(-0.25f);
}
paint.setTypeface(tf);
}
}
答案 1 :(得分:0)
您要做的是创建一个扩展TextView的自定义类,然后在构造函数中设置您的默认字体。然后在xml中引用自定义textview而不是Androids TextView。