android - 设置背景图像扰乱选项卡 Activity 中的布局

标签 android tabs styles background-image

我正在进行一项 Activity ,其中包含选项卡。当我不使用应用程序背景作为带有以下代码的图像时,布局工作正常。

   <style name="AppTheme" parent="AppBaseTheme">
      <!--  <item name="android:background">@drawable/imagese4copy</item>
         All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

布局看起来像:
enter image description here

当我包含背景图片时,它看起来像:
enter image description here

这是我的 MainActivity.java

public class MainActivity extends  TabActivity {
    private TabHost tabHost;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Resources resource = getResources();
        tabHost = (TabHost)findViewById(android.R.id.tabhost);
        tabHost.setup();


        Intent intentHelper = new Intent(this, Helper.class);
        TabSpec tabSpecHelper = tabHost
                .newTabSpec("Helper")
                .setIndicator("",
                        resource.getDrawable(R.drawable.ic_launcher))
                .setContent(intentHelper);

        Intent intentRequest = new Intent(this, HelpRequest.class);
        TabSpec tabSpecRequest = tabHost
                .newTabSpec("HelpRequest")
                .setIndicator("",
                        resource.getDrawable(R.drawable.ic_launcher))
                .setContent(intentRequest);

        Intent intentSetting = new Intent(this, SettingsActivity.class);
        TabSpec tabSpecSetting = tabHost
                .newTabSpec("Setting")
                .setIndicator("",
                        resource.getDrawable(R.drawable.ic_launcher))
                .setContent(intentSetting);

        tabHost.addTab(tabSpecHelper);
        tabHost.addTab(tabSpecRequest);
        tabHost.addTab(tabSpecSetting);
        tabHost.setCurrentTab(0);
    }

activity_main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
</TabHost>

AndroidManifest.xml:

  <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.maptest.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.maptest.HelpRequest"
            android:label="@string/title_activity_help_request" >
        </activity>
        <activity
            android:name="com.example.maptest.Helper"
            android:label="@string/title_activity_helper" >
        </activity>
        <activity
            android:name="com.example.maptest.SettingsActivity"
            android:label="@string/title_activity_settings" >
        </activity>
    </application>

</manifest>

请告诉我包含背景图片时出了什么问题,我该如何纠正。

问候,
Sourabh

最佳答案

经过研究 android dev guide我发现如果您在应用程序中应用背景图像,它将为应用程序中的所有 View 设置背景。由于 Tab 和 TextView 是一个 View ,它们也有 backgroundImage,因此会扰乱布局。

我不知道是否有更完美的解决方案可用,但将背景图像设置为 LinearLayout 或 RelativeLayout 将解决问题,但您必须将此应用于每个 xml 文件才能在所有 Activity 中获得相同的背景。

例子:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background_darkgreyrough"
    tools:context=".Overview" >

关于android - 设置背景图像扰乱选项卡 Activity 中的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17930896/

相关文章:

javascript - magento jquery toggle 不能在主页上工作但在产品页面上工作

java - 添加日期选择器后时间选择器停止工作

javascript - jQuery 选项卡 : Disable window location hash

android - 是否可以禁用 ViewPager 上的滚动

google-chrome - 谷歌浏览器禁用标签工具提示弹出窗口

VBA 高级自动筛选 + 根据范围创建新工作表

wpf - 在 WPF 中显示 GIF 动画图像的最佳方式是什么?

Android自定义按钮样式不起作用

android - 能不能避免在View名称前使用 "R.id"?

java - org.apache.http.entity.FileEntity在Android 6中已弃用(棉花糖)