我在MainActivity中调用getDrawable()时遇到了一个问题,我已经完成了activity_main.xml,但它显示了这样的警告:“来自类型资源的方法getDrawable(int)已被弃用”。
有谁可以帮我?
MainActivity.java
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
<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>
发布于 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(),并接受弃用通知。
https://stackoverflow.com/questions/30535965
复制相似问题