Warum Sie zwei registrieren außer Kraft setzen Aktivität in Manifest? Der Name beider Layout-Dateien (Land + Port) muss identisch sein.
Auf diese Weise wählt Ihre Aktivität aus, welches Layout automatisch ausgewählt wird.
Wichtig: - Aber bevor Sie Ihre Layouts und das Hinzufügen von Hörern auf Ansichten in Aktivitätsklasse Hinzufügen von Referenz müssen Sie überprüfen, welche aktuelle Ausrichtung andere weise sie in Nullpointer Absturz führen wird, wie Sie anhand der zu ergreifen, wird versuchen, die ist nicht aufgeblasen.
Was müssen Sie tun: -
- Entfernen Landschaft Aktivität von manifest
- Name ändern Landschaftsbelegung wie Porträt und verschieben Sie sie Ressourcenverzeichnis Layout-Land-Ordner in.
- Bevor Sie findviewbyId in der Aktivitätsklasse verwenden, müssen Sie prüfen, ob die aktuelle Ausrichtung Land oder Port ist.
Beispiel Arbeits-Code
Hauptaktivitätsklasse
public class HomeActivity erweitert FragmentActivity {
private UniversalWebViewFragment detailFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_activity);
ListView listView = (ListView) findViewById(R.id.selector);
final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
//Load random Google search URLS for List
list.add("https://www.google.com/search?q=" + i);
}
listView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, list));
if (!isPortrait(getApplicationContext())) {
detailFragment = (UniversalWebViewFragment) getSupportFragmentManager().findFragmentById(R.id.webView);
}
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
if (isPortrait(getApplicationContext())) {
Toast.makeText(getApplicationContext(), "Do Fragment Transition with URL" + list.get(arg2), 300).show();
} else {
if (null != detailFragment)
detailFragment.loadURL(list.get(arg2));
}
}
});
}
//Check if device is in portrait mode
public static boolean isPortrait(Context context) {
return context.getResources().getBoolean(R.bool.is_portrait);
}
}
res/layout/home_activity.xml
<ListView
android:id="@+id/selector"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true" >
</ListView>
res/Layout-land/home_activity.xml
<ListView
android:id="@+id/selector"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true" >
</ListView>
<fragment
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/selector"
class="com.masterdetail_webview.UniversalWebViewFragment" />
Detektiervorrichtung oder ientation
res/Werte/layouts.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="is_portrait">true</bool>
</resources>
res/Werte-Land/layouts.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="is_portrait">false</bool>
</resources>
Extras zeigt Webinhalte mit Hilfe vonUniversalWebViewFragment
Laden von Webcontents in Fragment einfach diese Klasse fallen lassen in Ihrem Projekt
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
@SuppressLint("NewApi")
public class UniversalWebViewFragment extends Fragment {
public static final String WEB_URL_TO_LOAD = "webURLToLoad";
private static final String GOOGLE_SERACH_URL = "https://www.google.com/search?q=";
private WebView webView;
private FrameLayout customViewContainer;
private WebChromeClient.CustomViewCallback customViewCallback;
private View mCustomView;
private myWebChromeClient mWebChromeClient;
private myWebViewClient mWebViewClient;
public static UniversalWebViewFragment newInstance(String webUrl, boolean serachOnWeb) {
Bundle bdl = new Bundle();
if (serachOnWeb) {
// serach on google for query
bdl.putString(WEB_URL_TO_LOAD, GOOGLE_SERACH_URL + webUrl);
} else {
// seimply load url
bdl.putString(WEB_URL_TO_LOAD, webUrl);
}
UniversalWebViewFragment newInstance = new UniversalWebViewFragment();
newInstance.setArguments(bdl);
return newInstance;
}
/**
* Called when the activity is first created.
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.universal_web_view, container, false);
// if (null != getArguments() && null !=
// getArguments().getString(WEB_URL_TO_LOAD)) {
customViewContainer = (FrameLayout) rootView.findViewById(R.id.customViewContainer);
webView = (WebView) rootView.findViewById(R.id.webView);
mWebViewClient = new myWebViewClient();
webView.setWebViewClient(mWebViewClient);
mWebChromeClient = new myWebChromeClient();
webView.setWebChromeClient(mWebChromeClient);
webView.getSettings().setJavaScriptEnabled(true);
// Important for PayUMoney
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSaveFormData(true);
loadURL(GOOGLE_SERACH_URL);
// webView.requestFocus();
// Handle Back keyPress
rootView.setFocusableInTouchMode(true);
rootView.requestFocus();
rootView.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (inCustomView()) {
hideCustomView();
getActivity().getSupportFragmentManager().popBackStack();
return true;
}
if ((mCustomView == null) && webView.canGoBack()) {
webView.goBack();
getActivity().getSupportFragmentManager().popBackStack();
return true;
}
}
// return super.onKeyDown(keyCode, event);
return true;
}
});
// }
return rootView;
}
/**
*
*/
public void loadURL(String URl) {
webView.loadUrl(URl);
}
public boolean inCustomView() {
return (mCustomView != null);
}
public void hideCustomView() {
mWebChromeClient.onHideCustomView();
}
@Override
public void onPause() {
super.onPause(); // To change body of overridden methods use File |
// Settings | File Templates.
webView.onPause();
}
@Override
public void onResume() {
super.onResume(); // To change body of overridden methods use File |
// Settings | File Templates.
webView.onResume();
}
@Override
public void onStop() {
super.onStop(); // To change body of overridden methods use File |
// Settings | File Templates.
if (inCustomView()) {
hideCustomView();
}
}
class myWebChromeClient extends WebChromeClient {
private View mVideoProgressView;
@Override
public void onShowCustomView(View view, int requestedOrientation, CustomViewCallback callback) {
onShowCustomView(view, callback); // To change body of overridden
// methods use File | Settings |
// File Templates.
}
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
// if a view already exists then immediately terminate the new one
if (mCustomView != null) {
callback.onCustomViewHidden();
return;
}
mCustomView = view;
webView.setVisibility(View.GONE);
customViewContainer.setVisibility(View.VISIBLE);
customViewContainer.addView(view);
customViewCallback = callback;
}
@Override
public View getVideoLoadingProgressView() {
if (mVideoProgressView == null) {
LayoutInflater inflater = LayoutInflater.from(getActivity());
mVideoProgressView = inflater.inflate(R.layout.video_progress, null);
}
return mVideoProgressView;
}
@Override
public void onHideCustomView() {
super.onHideCustomView(); // To change body of overridden methods
// use File | Settings | File Templates.
if (mCustomView == null)
return;
webView.setVisibility(View.VISIBLE);
customViewContainer.setVisibility(View.GONE);
// Hide the custom view.
mCustomView.setVisibility(View.GONE);
// Remove the custom view from its container.
customViewContainer.removeView(mCustomView);
customViewCallback.onCustomViewHidden();
mCustomView = null;
}
}
class myWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
private int webViewPreviousState;
private final int PAGE_STARTED = 0x1;
private final int PAGE_REDIRECTED = 0x2;
Dialog dialog = new Dialog(getActivity());
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
webViewPreviousState = PAGE_STARTED;
if (dialog == null || !dialog.isShowing())
dialog = ProgressDialog.show(getActivity(), "", "Loading Please Wait", true, true,
new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// do something
}
});
}
@Override
public void onPageFinished(WebView view, String url) {
if (webViewPreviousState == PAGE_STARTED) {
if (null != dialog)
dialog.dismiss();
dialog = null;
}
}
}
}
res/layout/universal_web_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/webView"
android:layout_gravity="center"
/>
<FrameLayout
android:id="@+id/customViewContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone"
/>
</LinearLayout>
Extra-Layout für youtube Fortschritt (nur für den Fall)
res/video_progress
<ProgressBar android:id="@android:id/progress"
style="?android:attr/progressBarStyleLarge"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:paddingTop="5dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="loading"
android:textSize="14sp"
android:textColor="?android:attr/textColorPrimary"/>
Ergebnis: - Ich in gesicherter Proxy bin Netzwerk wird es in offenen n schön aussehen et
Ihre Portait und Landschaft Layout in derselben Aktivität aufgeblasen? Wenn ja, fügen Sie bitte auch den Aktivitätscode hinzu. – USKMobility
http://developer.android.com/training/basics/supporting-devices/screens.html – USKMobility
Ich ändere nur einige Dateien umbenennen, falls es zu Verwirrung kommt. Ich denke, das Hauptproblem ist, dass der Emulator das Landschaftslayout nicht finden kann. – Albert