更新:我认为它工作正常。 但经过一些testing麻烦依然存在*嗅探* 然后,我做了一个更简单的版本,看看究竟发生了什么,我才知道应该被分离的清爽片段仍然留在那里。 或者确切地说,旧片段的视图在新片段的顶部 。 由于RecyclerView的我的原始应用程序的背景是不透明的,所以原来我之前说过的。 更新结束 我有这样的布局的MainActivity : <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <!– As the main content view, the view below consumes the entire space available using match_parent in both dimensions. –> <FrameLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" /> <!– android:layout_gravity="start" tells DrawerLayout to treat this as a sliding drawer on the left side […]
我一直在使用支持库中包含的新的嵌套的片段 API。 我面临的嵌套片段的问题是,如果嵌套片段(即,已经通过由getChildFragmentManager()返回的FragmentManager添加到另一个片段的FragmentManager )调用startActivityForResult() ,则嵌套片段的onActivityResult()方法不叫。 但是,父级片段的onActivityResult()和activity的onActivityResult()都会被调用。 我不知道我是否错过了一些关于嵌套片段的东西,但我没有想到所描述的行为。 下面是重现这个问题的代码。 我非常感谢,如果有人能指出我正确的方向,并向我解释我做错了什么: package com.example.nestedfragmentactivityresult; import android.media.RingtoneManager; import android.os.Bundle; import android.content.Intent; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.Toast; public class MainActivity extends FragmentActivity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.getSupportFragmentManager() .beginTransaction() .add(android.R.id.content, new ContainerFragment()) .commit(); } public void onActivityResult(int requestCode, int […]
我有一个应用程序,有四个不同的活动。 现在我想在应用程序中添加一个导航抽屉来导航这些活动。 任何人都可以给出一个例子或导航抽屉教程为不同的活动与背单词兼容性。我已经看到了片段的例子,但我需要活动的例子。
更新:解决! 问题与我的Viewpager而不是WebView有关。 我想添加一个“返回”function,我的WebView里面的Fragment 。 但我不知道如何: public final class TestFragment extends Fragment { static WebView mWeb; private View mContentView; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mContentView = inflater.inflate(R.layout.webview, null); mWeb = (WebView)mContentView.findViewById(R.id.webview); WebSettings settings = mWeb.getSettings(); settings.setJavaScriptEnabled(true); settings.setSupportZoom(false); mWeb.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); mWeb.getSettings().setBuiltInZoomControls(false); mWeb.loadUrl("myurl…"); mWeb.setOnKeyListener(new OnKeyListener(){ public boolean onKey(View v, int keyCode, KeyEvent event) { […]
我正在开发包含2个片段的应用程序,我想根据我的需要显示隐藏。 下面的代码是我的问题的简单例子。 这个简单的Fragmentactivity包含1个button和一个listfragment。 这个简单的例子工作完美无瑕。 但是我不满意show hide片段。 如果你删除layout.setVisibility(View.GONE); 从代码然后ft.hide(f); 不会隐藏片段。 实际上我们并没有隐藏我们隐藏容器的片段。 我的问题是,这是一个显示隐藏碎片的方法吗? 如果不是,那么请解释一下testing的例子,因为很多人都面临这个问题,所以如何隐藏和显示碎片。 public class MainActivity extends FragmentActivity implements OnClickListener { Fragment1 f; Button b; LinearLayout layout; Fragment myf; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b = (Button) findViewById(R.id.button1); layout = (LinearLayout) findViewById(R.id.ll); f = new Fragment1(); } @Override public void onClick(View v) { FragmentTransaction […]
我有一个片段打开一个Dialogfragment获取用户input(一个string和一个整数)。 我如何将这两件事发回给片段? 这是我的DialogFragment: public class DatePickerFragment extends DialogFragment { String Month; int Year; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { getDialog().setTitle(getString(R.string.Date_Picker)); View v = inflater.inflate(R.layout.date_picker_dialog, container, false); Spinner months = (Spinner) v.findViewById(R.id.months_spinner); ArrayAdapter<CharSequence> monthadapter = ArrayAdapter.createFromResource(getActivity(), R.array.Months, R.layout.picker_row); months.setAdapter(monthadapter); months.setOnItemSelectedListener(new OnItemSelectedListener(){ @Override public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int monthplace, long id) […]
我正在尝试在DialogFragment使用自定义视图创build一个AlertDialog 。 这个视图必须从xml中夸大。 在我的DialogFragment类中,我有: @Override public Dialog onCreateDialog(Bundle savedInstanceState) { return new AlertDialog.Builder(getActivity()) .setTitle("Title") .setView(getActivity().getLayoutInflater().inflate(R.layout.dialog, null)) .setPositiveButton(android.R.string.ok, this) .setNegativeButton(android.R.string.cancel, null) .create(); } 我已经尝试其他通胀方法.setView()如: .setView(getActivity().getLayoutInflater().inflate(R.layout.dialog, (ViewGroup) getView(), false)) 和 .setView(getActivity().getLayoutInflater().inflate(R.layout.dialog, (ViewGroup) getTargetFragment().getView(), false)) 在显示此对话框的片段中设置目标片段之后。 所有这些尝试膨胀我的自定义视图结果在以下例外: E/AndroidRuntime(32352): android.util.AndroidRuntimeException: requestFeature() must be called before adding content E/AndroidRuntime(32352): at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:214) E/AndroidRuntime(32352): at com.android.internal.app.AlertController.installContent(AlertController.java:248) E/AndroidRuntime(32352): at android.app.AlertDialog.onCreate(AlertDialog.java:314) E/AndroidRuntime(32352): at android.app.Dialog.dispatchOnCreate(Dialog.java:335) […]
在我的主要FragmentActivity ,我设置我的自定义ActionBar标题是这样的: LayoutInflater inflator = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View v = inflator.inflate(R.layout.custom_titlebar, null); TextView tv = (TextView) v.findViewById(R.id.title); Typeface tf = Typeface.createFromAsset(this.getAssets(), "fonts/capsuula.ttf"); tv.setTypeface(tf); tv.setText(this.getTitle()); actionBar.setCustomView(v); 这工作完美。 但是,一旦我打开其他Fragments ,我希望标题改变。 我不知道如何访问主要Activity来做到这一点? 在过去,我这样做了: ((MainFragmentActivity) getActivity()).getSupportActionBar().setTitle( catTitle); 有人可以build议正确的方法吗? XML: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/transparent" > <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="5dp" android:ellipsize="end" android:maxLines="1" android:text="" android:textColor="#fff" […]
我在一个xml布局文件(我们称之为layout_mydialogfragment.xml )中指定了DialogFragment的布局,特别是它的layout_width和layout_height属性( 100dp每个都是100dp )。 然后我在DialogFragment的onCreateView(…)方法中膨胀这个布局,如下所示: View view = inflater.inflate(R.layout.layout_mydialogfragment, container, false); 不幸的是,我发现当我的对话框(DialogFragment)出现时,它不尊重在其xml布局文件中指定的layout_width和layout_height (并且我的对话框根据内容不断缩小或扩大)。 任何人都知道是否可以让我的对话框尊重在其布局文件中指定的layout_width和layout_height ? 此刻我不得不在我的DialogFragment的onResume()方法中再次指定我的对话框的宽度和高度,如下所示… getDialog().getWindow().setLayout(width, height); …因此,不必要的是,必须记住将来对对话的宽度和高度进行两处修改。
我正在开发一个使用Android 4.0 ICS和片段的应用程序。 从ICS 4.0.3(API level 15)API的演示示例应用程序考虑这个修改的示例: public class FragmentTabs extends Activity { private static final String TAG = FragmentTabs.class.getSimpleName(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); final ActionBar bar = getActionBar(); bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE); bar.addTab(bar.newTab() .setText("Simple") .setTabListener(new TabListener<SimpleFragment>( this, "mysimple", SimpleFragment.class))); if (savedInstanceState != null) { bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0)); Log.d(TAG, "FragmentTabs.onCreate tab: " + savedInstanceState.getInt("tab")); […]