2015-01-08 20 views
7

Ich versuche eine App zu erstellen, die zwei verschiedene UIs für Tablets und Telefone hat, und ich verwende Fragmente, um dies zu implementieren. Ich habe zwei separate XML-Dateien für jedes Tablet- und Telefonlayout erstellt und beide heißen activity_main.xml, wobei das Telefonlayout im Ordner res/layout abgelegt wird und das tablet-Layout im Ordner res/layout-sw600dp abgelegt wird.Tablet UI funktioniert nicht auf meiner App

Wenn ich jedoch versuche, meine App auf einem Nexus 10-Emulator (Android Studio) auszuführen, wird automatisch das Standardtelefonlayout angezeigt. Die App stürzt nicht ab, aber sie wird nur in der Telefonbenutzeroberfläche ausgeführt, wenn sie in der Tablet-Benutzeroberfläche ausgeführt werden soll. Ich weiß nicht, was die Quelle dieses Fehlers sein könnte, und so würde jede Hilfe geschätzt werden.

Hier ist ein Update, ich habe versucht, den Ordner res/layout-large-mdpi umbenennen, aber immer noch keine Änderung gefunden. Ist es möglich, dass es etwas mit dem Emulator zu tun hat?

Hier ist mein Manifest:

<?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 Layout:

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

normale Layout:

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

Und schließlich das ist meine MainActivity.java Klasse:

/* 
* 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!"); 
     } 
    } 
} 
+0

Haben Sie andere Ressourcenordner mit Qualifiern? Vielleicht hat einer davon Priorität gegenüber Ihrem sw600dp-Ordner? – stkent

+1

hast du es in einem echten Gerät versucht? In meinem Nexus 7 kann ich es mit Werten-sw6000dp archivieren. Vielleicht können Sie versuchen, Ordner Werte zu erstellen-sw720dp (10 "Tablette) –

+1

Haben Sie Layout-Large versucht? Das ist, was wir verwenden, um unsere Tablet-und Telefon-Layouts zu unterscheiden. Layout-Large bekommt Sie beide 7" und 10 "Tabletten, und wenn Sie brauchen etwas anderes für 10 "können Sie Layout-Xlarge verwenden. – Bruce

Antwort

-2

Die Verwendung von layout-xhdpi funktioniert für dieses spezifische Szenario und ein Nexus 7- oder 10-Tablet-Emulator für den Nanodegree. Ich konnte das Layout-sw600dp nie richtig auf meinem Emulator laden und stattdessen verwalte ich Sunshine mit einem Layout-xhdpi und einem Layout-sw600dp mit der gleichen activity_main.xml.

+0

Dies hat den unglücklichen Nebeneffekt, dass einige Telefone auch das Layout bekommen. Es muss eine bessere Lösung geben. –

0

Haben Sie versucht, was passiert mit dem Layout, wenn Sie das Gerät drehen? (ZB als pro Sonnenschein Projekt benötigen Sie sowohl das Layout-sw600dp-Port und Layout-sw600dp erstellen)

0

Layout-sw600dp ist für 7-Zoll-Tablet/Emulator

Layout-sw720dp ist für 10-Zoll-Tablet/Emulator

9

Für mich war der Code korrekt, aber das Problem war mit dem Emulator. Wenn Sie Ihren Emulator mit No skin einrichten, sollte es funktionieren. Entferne diese Haut!

+0

wirklich eine Herausforderung für Anfänger – wakandan

Verwandte Themen