Android UI TabActivity问题

发布于 2024-10-30 05:30:38 字数 2062 浏览 1 评论 0原文

我正在尝试为该应用程序实现以下背景...... 在此处输入图像描述

对于背景图像(应用程序背景)...我正在 setContentView(layout) 中设置图像。 ..通过添加这一行,我收到运行时异常...

如果我在子活动中设置此背景..我不会让背景填充完整的应用程序背景..知道还有什么替代方法吗?

public class HMITabActivity extends TabActivity{
    private TabHost tabHost = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.background);
        tabHost = getTabHost();
        tabHost.setOnTabChangedListener(new OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                setTabHostColors();
            }
        });
        tabHost.addTab(tabHost.newTabSpec("Tasks")
                .setIndicator("Tasks", getResources().getDrawable(R.drawable.icon_task))
                .setContent(new Intent(this, Tasks.class)));
        tabHost.addTab(tabHost.newTabSpec("HMI")
                .setIndicator("HMI", getResources().getDrawable(R.drawable.icon_hmi))
                .setContent(new Intent(this, HMI.class)));
        tabHost.addTab(tabHost.newTabSpec("Diagnostics")
                .setIndicator("Diagnostics", getResources().getDrawable(R.drawable.icon_diagnostics))
                .setContent(new Intent(this, Diagnostics.class)));
        tabHost.addTab(tabHost.newTabSpec("About")
                .setIndicator("About", getResources().getDrawable(R.drawable.icon_info))
                .setContent(new Intent(this, Tasks.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));

        Intent intent = new Intent(BackgroundService.class.getName());
        startService(intent); 
    }

    private void setTabHostColors() {
        for(int i=0;i<tabHost.getTabWidget().getChildCount();i++) {
            tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.rgb(1, 1, 1)); //unselected
        }
        tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.rgb(50, 120, 160)); // selected
    }

}

I am trying to implement the following background for the application...
enter image description here

For the background image(application background) ... i am setting the image in setContentView(layout)... by adding this line, i am getting a runtime exception...

if i set this background in the subactivities..i wont get the background to fill the full application background.. any idea whats the alternative?

public class HMITabActivity extends TabActivity{
    private TabHost tabHost = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.background);
        tabHost = getTabHost();
        tabHost.setOnTabChangedListener(new OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                setTabHostColors();
            }
        });
        tabHost.addTab(tabHost.newTabSpec("Tasks")
                .setIndicator("Tasks", getResources().getDrawable(R.drawable.icon_task))
                .setContent(new Intent(this, Tasks.class)));
        tabHost.addTab(tabHost.newTabSpec("HMI")
                .setIndicator("HMI", getResources().getDrawable(R.drawable.icon_hmi))
                .setContent(new Intent(this, HMI.class)));
        tabHost.addTab(tabHost.newTabSpec("Diagnostics")
                .setIndicator("Diagnostics", getResources().getDrawable(R.drawable.icon_diagnostics))
                .setContent(new Intent(this, Diagnostics.class)));
        tabHost.addTab(tabHost.newTabSpec("About")
                .setIndicator("About", getResources().getDrawable(R.drawable.icon_info))
                .setContent(new Intent(this, Tasks.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));

        Intent intent = new Intent(BackgroundService.class.getName());
        startService(intent); 
    }

    private void setTabHostColors() {
        for(int i=0;i<tabHost.getTabWidget().getChildCount();i++) {
            tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.rgb(1, 1, 1)); //unselected
        }
        tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.rgb(50, 120, 160)); // selected
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

对不⑦ 2024-11-06 05:30:38

为此,您必须使用自定义选项卡,这里是代码,请尝试以下操作:

  tabHost= getTabHost();
  tabHost.addTab(tabHost.newTabSpec("tab1").setContent(new Intent(this, Activity2.class)).setIndicator(prepareTabView("Names",R.drawable.icon)));

其中prepareTabView是Inflate View的方法。
然后膨胀一个像这样的视图:

    private View prepareTabView(String text, int resId) {
         View view = LayoutInflater.from(this).inflate(R.layout.tabs, null);
         ImageView iv = (ImageView) view.findViewById(R.id.TabImageView);
         TextView tv = (TextView) view.findViewById(R.id.TabTextView);
         iv.setImageResource(resId);
         tv.setText(text);
         return view;
    }

其中选项卡 XML 看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:id="@+id/TabLayout" 
android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:padding="5dip">

<ImageView android:id="@+id/TabImageView" android:src="@drawable/icon"           
 android:layout_width="wrap_content" android:layout_height="wrap_content"/>

<TextView android:id="@+id/TabTextView" android:text="Text" 
android:paddingTop="5dip" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textColor="@color/black" 
android:textAppearance="@style/TabTextViewStyle" />

 </LinearLayout>

然后现在根据需要添加背景颜色。

For this you must use Custom Tabs ,here is the Code try this :

  tabHost= getTabHost();
  tabHost.addTab(tabHost.newTabSpec("tab1").setContent(new Intent(this, Activity2.class)).setIndicator(prepareTabView("Names",R.drawable.icon)));

where prepareTabView is method that Inflate View.
Then Inflate a view like this :

    private View prepareTabView(String text, int resId) {
         View view = LayoutInflater.from(this).inflate(R.layout.tabs, null);
         ImageView iv = (ImageView) view.findViewById(R.id.TabImageView);
         TextView tv = (TextView) view.findViewById(R.id.TabTextView);
         iv.setImageResource(resId);
         tv.setText(text);
         return view;
    }

Where tabs XML will look like this :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:id="@+id/TabLayout" 
android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:padding="5dip">

<ImageView android:id="@+id/TabImageView" android:src="@drawable/icon"           
 android:layout_width="wrap_content" android:layout_height="wrap_content"/>

<TextView android:id="@+id/TabTextView" android:text="Text" 
android:paddingTop="5dip" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textColor="@color/black" 
android:textAppearance="@style/TabTextViewStyle" />

 </LinearLayout>

Then now add your backgroung color as you like..

云之铃。 2024-11-06 05:30:38

TabActivity 从 Android 4.2、API 级别 17 开始已弃用。使用 Fragments 代替 TabActivity

TabActivity is Deprecated from Android 4.2,API level 17. Use Fragments instead of TabActivity

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文