在Android中将图像添加到选项卡
我开发了一个简单的Android应用程序,其中我使用片段实现了标签。。。但是我无法显示选项卡的图像。。我已经在下面附上了源代码。。请让我知道什么是错误 我的活动类选项卡DemoFragmentActivity.java在Android中将图像添加到选项卡,android,android-tabhost,android-image,Android,Android Tabhost,Android Image,我开发了一个简单的Android应用程序,其中我使用片段实现了标签。。。但是我无法显示选项卡的图像。。我已经在下面附上了源代码。。请让我知道什么是错误 我的活动类选项卡DemoFragmentActivity.java public class TabDemoFragmentActivity extends FragmentActivity { /** * Instance variable of type {@link FragmentTabHost}
public class TabDemoFragmentActivity extends FragmentActivity {
/**
* Instance variable of type {@link FragmentTabHost}
*/
private FragmentTabHost fragmentTabHost;
/**
* This is a call back method, which gets invoked
* when the instance of this class is created.
*
* <p>
* This method is used to set the tab host and
* add the tab host fragments to the screen,
* which acts as a UI.
* </P>
*/
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_fragment_main);
fragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
fragmentTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
fragmentTabHost.addTab(fragmentTabHost.newTabSpec("DropCall Details").setIndicator("DropCall Details",
getResources().getDrawable(R.drawable.drop_call_image)).setContent(intent), QoSDropCallDetailsFragment.class, null);
fragmentTabHost.addTab(fragmentTabHost.newTabSpec("Network Details").setIndicator("Network Details",
getResources().getDrawable(R.drawable.network_details)), QoSNetworkDetailsFragment.class, null);
}
}
公共类选项卡DemoFragmentActivity扩展了FragmentActivity{
/**
*类型为{@link FragmentTabHost}的实例变量
*/
私有FragmentTabHost FragmentTabHost;
/**
*这是一个回调方法,它被调用
*创建此类的实例时。
*
*
*此方法用于设置选项卡主机和
*将选项卡主机片段添加到屏幕,
*它充当用户界面。
*
*/
@凌驾
创建时受保护的void(捆绑){
super.onCreate(bundle);
setContentView(R.layout.activity\u fragment\u main);
fragmentTabHost=(fragmentTabHost)findViewById(android.R.id.tabhost);
fragmentTabHost.setup(这个,getSupportFragmentManager(),R.id.realtabcontent);
fragmentTabHost.addTab(fragmentTabHost.newTabSpec(“DropCall Details”).setIndicator(“DropCall Details”),
getResources().getDrawable(R.drawable.drop_call_image)).setContent(intent),QoSDropCallDetailsFragment.class,null);
fragmentTabHost.addTab(fragmentTabHost.newTabSpec(“网络详细信息”).setIndicator(“网络详细信息”),
getResources().getDrawable(R.drawable.network_details)),QoSNetworkDetailsFragment.class,null);
}
}
我的XMl文件
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
<FrameLayout
android:id="@+id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50" />
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
此处未显示图像。。请告诉我如何在选项卡中显示图像。。谢谢我知道这是个老问题,但希望有人能登上这一页。首先,让我指出FragmentTabHost是不推荐的在应用程序中添加选项卡的方法。谷歌希望你移动到ActionTabBar来添加标签 我已经做了一个与您非常相似的实现,我看不出有什么不同。然而,我可以从你的代码中指出两件事:首先,你需要确保你将你的图像复制到所有3个可绘制文件夹中。第二,删除xml中额外的Framelayout(tabcontent),因为我只有一个Framelayout,而在Activity类中没有看到使用另一个Framelayout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
<FrameLayout
android:id="@+id/tabFrameLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
mTabHost = (FragmentTabHost) findViewById(R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.tabFrameLayout);
mTabHost.addTab(
mTabHost.newTabSpec("tab1").setIndicator("Tab 1",
getResources().getDrawable(R.drawable.user_card)),
TabActivity.ListFragment.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab2").setIndicator("Tab 2",
getResources().getDrawable(R.drawable.menu)),
TabActivity.ListFragments.class, null);
}