我正在为我的应用程序使用标签布局,但是当我尝试使用它时,我想确保我为标签布局设置的文本应该居中对齐,我尝试了不同的教程,其中我已成功但有一些问题。每当我点击标签时,标签的文字会略微向上移动,使我的屏幕也会受到干扰,当我第一次点击该屏幕时,标签下方会出现灰色线条。我使用了android:tabStripEnabled="false"
,但没有用
这是我的xml
<TabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:background="@drawable/buttonclientoption"
android:layout_gravity="center"
android:tabStripEnabled="false"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
这是我的代码
public TabHost tabHost;
public int currentTab = 0;
public int lastTab = 0;
public static final String caseno = "";
public static final String amountcalc = "";
String useridmain1 = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.clientoption);
getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getActionBar().setCustomView(R.layout.header);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
TextView tt1 = (TextView) findViewById(R.id.textView1header);
tt1.setText("My Client");
Bundle extras = getIntent().getExtras();
if (extras != null) {
String useridmain = extras.getString("homefraguserid");
System.out.println("useridhome" + useridmain);
useridmain1 = useridmain;
}
// ActionBar bar = getActionBar();
// bar.setBackgroundDrawable(new
// ColorDrawable(Color.parseColor("#058B96")));
final TabHost tabHost = getTabHost();
// tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#ffffff"));
// Tab for Photos
TabSpec photospec = tabHost.newTabSpec("New Business");
photospec.setIndicator("New Business");
Intent photosIntent = new Intent(this, NewBuisness.class);
photospec.setContent(photosIntent);
// Tab for Songs
TabSpec songspec = tabHost.newTabSpec("Client Services");
// setting Title and Icon for the Tab
// songspec.setIndicator("Client Services");
songspec.setIndicator("Client Services");
Intent songsIntent = new Intent(this, Clientservices.class);
songsIntent.putExtra("username", useridmain1);
songspec.setContent(songsIntent);
// Tab for Videos
tabHost.addTab(photospec); // Adding photos tab
tabHost.addTab(songspec);
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
tabHost.getTabWidget().getChildAt(i)
.setBackgroundColor(Color.parseColor("#ffffff"));
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i)
.findViewById(android.R.id.title); // Unselected Tabs
tv.setTextColor(Color.parseColor("#058B96"));
tv.setGravity(0);
}
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())
.setBackgroundColor(Color.parseColor("#058B96")); // selected
TextView tv = (TextView) tabHost.getCurrentTabView().findViewById(
android.R.id.title); // for Selected Tab
tv.setTextColor(Color.parseColor("#ffffff"));
tv.setGravity(0);
int tabCount2 = tabHost.getTabWidget().getTabCount();
for (int i = 0; i < tabCount2; i++) {
final View view = tabHost.getTabWidget().getChildTabViewAt(i);
if (view != null) {
// reduce height of the tab
view.getLayoutParams().height *= 0.60;
// get title text view
final View textView = view.findViewById(android.R.id.title);
if (textView instanceof TextView) {
// just in case check the type
// center text
((TextView) textView).setGravity(Gravity.CENTER);
// wrap text
((TextView) textView).setSingleLine(false);
// explicitly set layout parameters
textView.getLayoutParams().height = ViewGroup.LayoutParams.FILL_PARENT;
textView.getLayoutParams().width = ViewGroup.LayoutParams.WRAP_CONTENT;
}
}
}
// Tab for Videos
getTabHost().setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String tabId)
{
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++)
{
tabHost.getTabWidget().getChildAt(i)
.setBackgroundColor(Color.parseColor("#ffffff")); // unselected
TextView tv = (TextView) tabHost.getTabWidget()
.getChildAt(i).findViewById(android.R.id.title); // Unselected
// Tabs
tv.setTextColor(Color.parseColor("#058B96"));
tv.setGravity(0);
}
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())
.setBackgroundColor(Color.parseColor("#058B96")); // selected
TextView tv = (TextView) tabHost.getCurrentTabView()
.findViewById(android.R.id.title); // for Selected Tab
tv.setTextColor(Color.parseColor("#ffffff"));
tv.setGravity(0);
/*
*
* currentTab = getTabHost().getCurrentTab();
*
*
*
* setCurrentTabColor();
*
*
*
* lastTab = currentTab;
*/
}
});
getTabWidget().getChildAt(lastTab).setBackgroundColor(
Color.parseColor("#058B96"));
}
public void setCurrentTabColor() {
getTabWidget().getChildAt(currentTab).setBackgroundColor(
Color.parseColor("#058B96"));
getTabWidget().getChildAt(lastTab).setBackgroundColor(
Color.parseColor("#ffffff"));
}