我正在为tabwidget Tab扩展自定义布局。但是,一旦我为最后一个选项卡增加自定义布局,我的底部选项卡小部件就会全屏显示。
我想要的是:
发生了什么 :
TabActivity代码:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Check if GCM configuration is set
if (CommonUtilities.SERVER_URL == null || CommonUtilities.SENDER_ID == null || CommonUtilities.SERVER_URL.length() == 0
|| CommonUtilities.SENDER_ID.length() == 0) {
// GCM sernder id / server url is missing
Toast.makeText(MainActivity.this, "Configuration Error!", Toast.LENGTH_SHORT).show();
// stop executing code by return
return;
}
context = getApplicationContext();
// TabHost tabHost = getTabHost();
tabHost = getTabHost();
SharedPreferences preferences2 = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
String u_id1 = preferences2.getString("Userid","");
Bean b = new Bean();
b.setDeviceid(u_id1);
// Tab for Photos
TabSpec photospec = tabHost.newTabSpec("Favourite"); photospec.setIndicator(getLayoutInflater().inflate(R.layout.layout_favourites_tab,null));
Intent photosIntent = new Intent(this, Favourite.class);
photospec.setContent(photosIntent);
// Tab for Songs
TabSpec songspec = tabHost.newTabSpec("Contacts");
// setting Title and Icon for the Tab
songspec.setIndicator(getLayoutInflater().inflate(R.layout.layout_contacts_tab,null));
// songspec.setIndicator("Contacts", getResources().getDrawable(R.drawable.tab_contacts));
Intent songsIntent = new Intent(this, Contacts.class);
songspec.setContent(songsIntent);
// Tab for Videos
TabSpec videospec = tabHost.newTabSpec("Meeting");
videospec.setIndicator(getLayoutInflater().inflate(R.layout.layout_meeting_tab,null));
Intent videosIntent = new Intent(this, Meeting.class);
videospec.setContent(videosIntent);
// Tab for Future SMS
TabSpec futuresms = tabHost.newTabSpec("Map");
futuresms.setIndicator(getLayoutInflater().inflate(R.layout.layout_map_tab,null));
Intent futureIntent = new Intent(this, Map.class);
futuresms.setContent(futureIntent);
// Tab for Future SMS
//Inflating custom layout for all tabs
TabSpec setting = tabHost.newTabSpec("Setting");
setting.setIndicator(getLayoutInflater().inflate(R.layout.layout_setting_tab,null));
Intent settingintent = new Intent(this, Setting.class);
setting.setContent(settingintent);
// Adding all TabSpec to TabHost
tabHost.addTab(photospec); // Adding photos tab
tabHost.addTab(songspec); // Adding songs tab
tabHost.addTab(videospec); // Adding videos tab
tabHost.addTab(futuresms);
tabHost.addTab(setting);
for (int i = 0; i <tabHost.getTabWidget().getChildCount(); i++) {
tabHost.getTabWidget().getChildTabViewAt(i).setBackgroundColor(getResources().getColor(R.color.button_color));
}
tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tab_click);
String tabid = Bean.getDeviceid();
if(tabid == null)
{
int in = Integer.parseInt("2");
tabHost.setCurrentTab(in);
}
else
{
int in = Integer.parseInt(tabid);
tabHost.setCurrentTab(in);
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putString("msg","Hi");
super.onDestroy();
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
System.gc();
MainActivity.this.finish();
System.exit(0);
}
}
Tab.xml:
?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/transparent"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white"
>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="@android:color/white"
/>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="@color/button_color"
android:tabStripEnabled="false"
/>
</LinearLayout>
</TabHost>
Custom_layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="70dp"
android:layout_height="70dp"
android:orientation="horizontal"
android:background="@color/button_color"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="70dp"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:id="@+id/imageview"
android:theme="@style/tablayout"
android:background="@drawable/tab_setting"
android:layout_marginTop="10dp"
android:layout_gravity="center"/>
<android.support.v7.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Settings"
android:id="@+id/textview"
android:layout_gravity="center"
android:gravity="center"
android:textColor="@color/button_pressed"
android:textSize="10sp"
android:layout_marginTop="5dp"/>
</LinearLayout>
<View
android:id="@+id/view"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#2ca4dc" />
</LinearLayout>
请帮助我解决代码中的错误或其他方法。
最佳答案
这是由于android:layout_height =“ match_parent”使用wrap_content更改而发生的。
自定义布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="@color/colorPrimaryDark"
android:orientation="horizontal">
<LinearLayout
android:layout_width="70dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/imageview"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:background="@drawable/oryx_logo" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:gravity="center"
android:text="Settings"
android:textColor="@color/future_color"
android:textSize="10sp" />
</LinearLayout>
<View
android:id="@+id/view"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#2ca4dc" />
</LinearLayout>