【问题标题】:Tablet UI doesn't work on my app平板电脑 UI 不适用于我的应用
【发布时间】:2015-03-06 02:22:47
【问题描述】:

我正在尝试为平板电脑和手机创建一个具有两种不同 UI 的应用,并且我正在使用片段来实现它。我为每个平板电脑和手机布局创建了两个单独的 xml 文件,它们都称为 activity_main.xml,手机布局放置在 res/layout 文件夹中,平板电脑布局放置在 res/layout-sw600dp 文件夹中。

但是,当我尝试在 Nexus 10 模拟器 (Android Studio) 上运行我的应用时,它会自动转到默认手机布局。该应用程序不会崩溃或发生任何事情,但它只是在应该在平板电脑 UI 中运行时在手机 UI 中运行。我不知道这个错误的来源可能是什么,所以任何帮助将不胜感激。

这是一个更新,我尝试将文件夹重命名为 res/layout-large-mdpi,但仍然没有发现任何变化。会不会跟模拟器有关系?

这是我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidattack.www.sunshine" >

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.androidattack.www.sunshine.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.androidattack.www.sunshine.DetailActivity"
            android:label="@string/title_activity_detail"
            android:parentActivityName="com.androidattack.www.sunshine.MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.androidattack.www.sunshine.MainActivity" />
        </activity>
        <activity
            android:name="com.androidattack.www.sunshine.SettingsActivity"
            android:label="@string/title_activity_settings"
            android:parentActivityName="com.androidattack.www.sunshine.MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.androidattack.www.sunshine.MainActivity" />
        </activity>
        <provider
            android:authorities="com.androidattack.www.sunshine"
            android:name=".data.WeatherProvider" />
    </application>

</manifest>

sw600dp 布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="?android:attr/dividerHorizontal"
    android:baselineAligned="false"
    tools:context="com.androidattack.www.sunshine.MainActivity">

    <fragment
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:name="com.androidattack.www.sunshine.ForecastFragment"
        android:id="@+id/fragment_forecast"
        android:layout_gravity="center_vertical"
        android:layout_weight="1" />

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="2"
        android:id="@+id/weather_detail_container">

    </FrameLayout>


</LinearLayout>

正常布局:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_forecast"
    android:name="com.androidattack.www.sunshine.ForecastFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    tools:context="com.androidattack.www.sunshine.ForecastFragment"
    tools:ignore="MergeRootFrame"
    tools:layout="@android:layout/list_content" />

最后这是我的 MainActivity.java 类:

/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.androidattack.www.sunshine;

import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity {

    private final String LOG_TAG = MainActivity.class.getSimpleName();
    private boolean mTwoPane;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (findViewById(R.id.weather_detail_container) != null) {
            // Tablet view
            mTwoPane = true;

            // In two-pane mode, show the detail view by adding
            // or replacing the detail fragment using a
            // fragment transaction.

            if (savedInstanceState == null) {
                getSupportFragmentManager().beginTransaction()
                        .replace(R.id.weather_detail_container, new DetailFragment())
                        .commit();
            }
        } else {
            // Phone View
            mTwoPane = false;
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            startActivity(new Intent(this, SettingsActivity.class));
            return true;
        }
        if (id == R.id.action_map) {
            openPreferredLocationInMap();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void openPreferredLocationInMap() {
        SharedPreferences sharedPrefs =
                PreferenceManager.getDefaultSharedPreferences(this);
        String location = sharedPrefs.getString(
                getString(R.string.pref_location_key),
                getString(R.string.pref_location_default));

        // Using the URI scheme for showing a location found on a map.  This super-handy
        // intent can is detailed in the "Common Intents" page of Android's developer site:
        // http://developer.android.com/guide/components/intents-common.html#Maps
        Uri geoLocation = Uri.parse("geo:0,0?").buildUpon()
                .appendQueryParameter("q", location)
                .build();

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(geoLocation);

        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        } else {
            Log.d(LOG_TAG, "Couldn't call " + location + ", no receiving apps installed!");
        }
    }
}

【问题讨论】:

  • 您还有其他带有限定符的资源文件夹吗?也许其中一个优先于您的 sw600dp 文件夹?
  • 你在真机上试过了吗?在我的 nexus 7 中,我可以使用 values-sw6000dp 将其存档。也许你可以尝试创建文件夹 values-sw720dp (10" tablet)
  • 你试过layout-large吗?这就是我们用来区分平板电脑和手机布局的方法。 layout-large 为您提供 7" 和 10" 平板电脑,如果您需要不同的 10" 平板电脑,您可以使用 layout-xlarge。
  • 从技术上讲,我不需要创建 sw720dp 文件夹或 layout-xlarge,因为它们的最短宽度都超过 600dp,并且应该符合 sw600dp 的标准。所以包括 7 英寸平板电脑在内的所有平板电脑都应该可以工作,不,我没有在真正的平板电脑上尝试过,因为我没有。

标签: java android android-layout android-fragments


【解决方案1】:

使用 layout-xhdpi 将适用于这个特定的场景,而 Nexus 7 或 10 平板电脑模拟器则适用于 Nanodegree。我永远无法让layout-sw600dp 正确加载到我的模拟器上,相反,我目前使用layout-xhdpi 和layout-sw600dp 维护Sunshine,并使用相同的activity_main.xml。

【讨论】:

  • 这具有使某些手机也获得布局的不幸副作用。必须有更好的解决方案。
【解决方案2】:

您是否尝试过旋转设备时布局会发生什么? (例如,根据阳光项目,您需要同时创建 layout-sw600dp-port 和 layout-sw600dp)

【讨论】:

    【解决方案3】:

    layout-sw600dp 适用于 7 英寸平板电脑/模拟器

    layout-sw720dp 适用于 10 英寸平板电脑/模拟器

    【讨论】:

      【解决方案4】:

      对我来说,代码是正确的,但问题出在模拟器上。如果你用No skin 设置你的模拟器,它应该可以工作。去掉那个皮肤!

      【讨论】:

      • 对于初学者来说真的是一个陷阱
      猜你喜欢
      • 2016-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多