gpt4 book ai didi

java - 如何更改从 PagerSlidingTabStrip 创建的选项卡标题的字体?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:31:13 27 4
gpt4 key购买 nike

这是有问题的库:https://github.com/astuetz/PagerSlidingTabStrip我认为它与 .setTypeface() 有关,但我不知道如何使用它。我把我想要的字体放在 Assets 文件夹中。这是我的代码。

启动.java

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.Editable;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.astuetz.PagerSlidingTabStrip;


public class Start extends FragmentActivity {

private final Handler handler = new Handler();
private PagerSlidingTabStrip tabs;
private ViewPager pager;
private MyPagerAdapter adapter;
private int currentColor = 0xFF547CC1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);

tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
pager = (ViewPager) findViewById(R.id.pager);
adapter = new MyPagerAdapter(getSupportFragmentManager());

pager.setAdapter(adapter);
final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources()
.getDisplayMetrics());
pager.setPageMargin(pageMargin);
tabs.setViewPager(pager);
tabs.setIndicatorColor(currentColor);
/*Typeface tf = Typeface.createFromAsset(getAssets(), "RobotoCondensed-Regular.ttf");
tabs.setTypeface(tf,Typeface.NORMAL);*/

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.start, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}else if(id == R.id.add_location){
Toast.makeText(getApplicationContext(), "todo", Toast.LENGTH_SHORT).show();
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Enter a location");
alert.setMessage("Enter a zipcode or city");

final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Editable value = input.getText();
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Canceled.
}
});

alert.show();
return true;
}
return super.onOptionsItemSelected(item);
}

@Override
protected void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState){
super.onRestoreInstanceState(savedInstanceState);
}

private Drawable.Callback drawableCallback = new Drawable.Callback() {
@Override
public void invalidateDrawable(Drawable who) {
getActionBar().setBackgroundDrawable(who);
}

@Override
public void scheduleDrawable(Drawable who, Runnable what, long when) {
handler.postAtTime(what, when);
}

@Override
public void unscheduleDrawable(Drawable who, Runnable what) {
handler.removeCallbacks(what);
}
};

public class MyPagerAdapter extends FragmentPagerAdapter{
private final String[] TABS = {"Today", "This Week"};

public MyPagerAdapter(FragmentManager fm){
super(fm);
}

public CharSequence getPageTitle(int position){
return TABS[position];
}

@Override
public int getCount() {
return TABS.length;
}

@Override
public Fragment getItem(int position) {
return WeatherTabs.newInstance(position);
}
}
}

activity_start.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.astuetz.PagerSlidingTabStrip
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="48dip"
app:pstsShouldExpand="true" />

<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tabs"
tools:context=".Start" />
</RelativeLayout>

正如您在 Start.java 中看到的那样,我已经尝试在 onCreate() 方法中使用 .setTypeface() 并且它没有产生任何错误,但是当我运行我的应用程序时它在启动时崩溃了所以我没有使用.setTypeface() 正确。

最佳答案

在您的 Activity 中添加以下代码即可:

com.viewpagerindicator.TabPageIndicator indicator = (com.viewpagerindicator.TabPageIndicator) findViewById(R.id.tabs);
Typeface type = Typeface.createFromAsset(getAssets(),"fonts/BYekan+ Bold.ttf");
LinearLayout view = (LinearLayout) indicator.getChildAt(0);
int tabCount=3;
for(int i=0;i<tabCount;i++){
TextView textView = (TextView) view.getChildAt(i);
textView.setTypeface(type);
}

关于java - 如何更改从 PagerSlidingTabStrip 创建的选项卡标题的字体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23273040/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com