【问题标题】:setting background Image disturbs the layout in tab activity设置背景图像扰乱选项卡活动中的布局
【发布时间】:2013-07-29 17:55:25
【问题描述】:

我有一个包含标签的活动。当我不使用应用程序背景作为具有以下代码的图像时,布局工作正常。

   <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>

布局看起来像:

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

这是我的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>

请告诉我当我包含背景图片时出了什么问题以及如何纠正它。

问候,
苏拉布

【问题讨论】:

  • 当你设置背景时代码有什么变化?
  • 当我添加 &lt;item name="android:background"&gt;@drawable/imagese4copy&lt;/item&gt; 并且不需要的布局出现时
  • 好的,但是你在哪里设置了样式?
  • 在清单文件中。实际上,我希望这个主题适用于所有活动。
  • 如果你尝试设置这个,用另一个主题,但里面没有 cmets,会发生什么?: 注意这不是答案,但我需要显示代码...

标签: android tabs styles background-image


【解决方案1】:

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

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

示例:

<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" >

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 2017-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多