我创建了一个滑动标签活动,我正在尝试动态更改应用栏的标题。但我做的事似乎不起作用!这是我的MainActivity.java:
public class MainActivity extends AppCompatActivity {
ViewPager mPager;
SlidingTabLayout mTabs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayShowHomeEnabled(true);
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
mTabs = (SlidingTabLayout) findViewById(R.id.tabs);
mTabs.setCustomTabView(R.layout.custom_tab_view, R.id.tabText);
mTabs.setDistributeEvenly(true);
mTabs.setSelectedIndicatorColors(getResources().getColor(R.color.colorAccent));
mTabs.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
mTabs.setViewPager(mPager);
}
class MyPagerAdapter extends FragmentPagerAdapter {
int tabIcons[] = {R.drawable.ic_events, R.drawable.ic_clients, R.drawable.ic_history};
String[] tabText = getResources().getStringArray(R.array.tabs);
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
getSupportActionBar().setTitle("Events");
EventsFragment eventsFragment = EventsFragment.getInstance(position);
return eventsFragment;
case 1:
getSupportActionBar().setTitle("Clients");
ClientsFragment clientsFragment = ClientsFragment.getInstance(position);
return clientsFragment;
case 2:
getSupportActionBar().setTitle("History");
HistoryFragment historyFragment = HistoryFragment.getInstance(position);
return historyFragment;
default:
getSupportActionBar().setTitle("Events");
EventsFragment defaultFragment = EventsFragment.getInstance(position);
return defaultFragment;
}
}
@Override
public CharSequence getPageTitle(int position) {
Drawable drawable = getResources().getDrawable(tabIcons[position]);
drawable.setBounds(0,0,64,64);
ImageSpan imageSpan = new ImageSpan(drawable);
SpannableString spannableString = new SpannableString(" ");
spannableString.setSpan(imageSpan, 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return spannableString;
}
@Override
public int getCount() {
return 3;
}
}
}
我还尝试使用以下方法在onCreate
方法中设置应用栏的标题:
getSupportActionBar().setTitle("Test");
也不起作用。
我该如何解决这个问题?谢谢!
答案 0 :(得分:2)
在 MainActivity 对象上调用 setTitle 方法。
在 MainActivity 内拨打电话:Private Sub saveFileDialog(saveType As String)
' Displays a SaveFileDialog
Dim saveFileDialog1 As New SaveFileDialog()
Select Case saveType
Case "PDF"
saveFileDialog1.Filter = "PDF File|*.pdf"
saveFileDialog1.Title = "Save a PDF File"
Case "Image"
saveFileDialog1.Filter = "PNG Image|*.png"
saveFileDialog1.Title = "Save an Image File"
End Select
saveFileDialog1.ShowDialog()
' If the file name is not an empty string open it for saving.
If saveFileDialog1.FileName <> "" Then
' Saves the Image via a FileStream created by the OpenFile method.
Dim fileStream As System.IO.Stream = saveFileDialog1.OpenFile()
Select Case saveType
Case "PDF"
Dim doc As iTextSharp.text.Document = New iTextSharp.text.Document(iTextSharp.text.PageSize.LETTER, Bounds.Left, Bounds.Right, Bounds.Top, Bounds.Bottom)
Dim wri As iTextSharp.text.pdf.PdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(doc, fileStream)
doc.Open()
Dim Image As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(bmp, System.Drawing.Imaging.ImageFormat.Png)
doc.Add(Image)
doc.Close()
Case "Image"
bmp.Save(fileStream, System.Drawing.Imaging.ImageFormat.Png)
End Select
fileStream.Close()
End If
End Sub
。从其他班级( MyPagerAdapter )拨打电话:setTitle(...)
活动是对 MainActivity 对象的引用。您可以使用activity.setTitle(...)
,它返回与片段关联的活动。
总之,使用getActivity()
来从片段中设置标题。
答案 1 :(得分:1)
这是我的一个项目的代码:
ActionBar actionBar = ((AboutActivity)context).getSupportActionBar();
actionBar.setCustomView(R.layout.action_bar_title_layout);
LinearLayout layout = (LinearLayout) actionBar.getCustomView();
TextView titleTextView = (TextView) layout.findViewById(R.id.page_title_layout);
titleTextView.setText("Your title");
titleTextView.setTypeface(Constants.TYPE_FACE_FONT_MEDIUM); // here you can customise your font
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
action_bar_title_layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/page_title_layout"
android:layout_width="wrap_content"
android:singleLine="true"
android:textSize="20dp"
android:textColor="@color/primary_ultralight"
android:text="Title"
android:alpha="0.9"
android:layout_height="wrap_content" />
</LinearLayout>