首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用getDrawable配置TabHost

问使用getDrawable配置TabHost
EN

Stack Overflow用户
提问于 2015-05-30 01:47:40
回答 1查看 275关注 0票数 1

我在MainActivity中调用getDrawable()时遇到了一个问题,我已经完成了activity_main.xml,但它显示了这样的警告:“来自类型资源的方法getDrawable(int)已被弃用”。

有谁可以帮我?

MainActivity.java

代码语言:javascript
复制
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Resources res= getResources();
         TabHost tabs = (TabHost) findViewById(android.R.id.tabhost);
         tabs.setup();
         TabHost.TabSpec spec = tabs.newTabSpec("pestana 1");
         spec.setContent(R.id.tab1);
         spec.setIndicator("1",res.getDrawable(android.R.drawable.bottom_bar));
         tabs.addTab(spec);

         tabs.setup();
         TabHost.TabSpec spec1 =tabs.newTabSpec("pestana 2");
         spec1.setContent(R.id.tab2);
         spec.setIndicator("2",res.getDrawable(android.R.drawable.btn_minus));
         tabs.addTab(spec1);

         tabs.setup();
         TabHost.TabSpec spec2 =tabs.newTabSpec("pestana 3");
         spec2.setContent(R.id.tab3);
         spec.setIndicator("3",res.getDrawable(android.R.drawable.btn_radio));
         tabs.addTab(spec2);
    }
}

activity_main.xml

代码语言:javascript
复制
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.tabhost1.MainActivity">

<TabHost
    android:id="@android: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="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="100dp"></TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical">

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="pestana 1"></TextView>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical">

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="Pesana 2"></TextView>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical">

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="Pesana 3"></TextView>
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>

​

EN

回答 1

Stack Overflow用户

发布于 2015-05-30 01:56:02

有人能帮我吗?

调用getDrawable()没有什么问题。对于约99%的Android设备,它是您唯一的选择。

在Android5.1中,他们添加了a second getDrawable() method, one that takes a theme as a second parameter。然后,他们将单参数版本的getDrawable()标记为不推荐使用。

如果您的minSdkVersion是22,请使用双参数版本的getDrawable()。

如果您的minSdkVersion小于22,请继续使用单参数版本的getDrawable(),并接受弃用通知。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30535965

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档