Questions tagged «android-fragments»

片段代表Android应用程序中可重用的行为或部分用户界面。

9
android-support-v4替代PreferenceFragment
当我意识到该库不支持PreferenceFragments时,我的应用程序开发突然中断了。菜鸟android开发人员可以使用任何替代方法来克服这一障碍吗? 到目前为止,这是我的主窗口 <?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"> <TabHost 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" > <FrameLayout android:id="@android:id/tabcontent" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0"/> <FrameLayout android:id="@+android:id/realtabcontent" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1"/> </LinearLayout> <TabWidget android:id="@android:id/tabs" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0" /> </TabHost> </LinearLayout> 对于我的TabActivity,我使用的是我在网上找到的东西。这是一个片段: public class TabControlActivity extends FragmentActivity implements TabHost.OnTabChangeListener { public …

4
如何在Android的导航抽屉中添加一个分隔符?
我有一个像这样的导航抽屉。我想添加一个部分分隔符(如分隔Neptune的行)。看起来很简单,但我在网上找不到对我的案子有用的任何东西。 这是我的MainActivity: public class MainActivity extends Activity { private DrawerLayout mDrawerLayout; private ListView mDrawerList; private ActionBarDrawerToggle mDrawerToggle; private CharSequence mDrawerTitle; private CharSequence mTitle; private String[] mPlanetTitles; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTitle = mDrawerTitle = getTitle(); mPlanetTitles = getResources().getStringArray(R.array.planets_array); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); mDrawerList = (ListView) findViewById(R.id.left_drawer); …

5
结束动画事件android
我在一个视图(位于片段内)中有一个淡入淡出的动画,每次动画完成后,该视图都会重新绘制自己。我发现周围有工作view.SetVisibility(View.GONE)。但是,它不会等待动画完成。我只想在动画完成后执行此setVisibility代码。最好的方法是什么?

21
从片段设置自定义ActionBar标题
在我的Main中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); 这很完美。但是,一旦我打开other Fragments,我想更改标题。我不确定如何访问MainActivity来执行此操作?过去,我这样做: ((MainFragmentActivity) getActivity()).getSupportActionBar().setTitle( catTitle); 有人可以建议适当的方法吗? 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="" …

9
片段中的Android SharedPreferences
我正在尝试阅读Fragment中的SharedPreferences。我的代码是我用来获取其他任何Activity的首选项的代码。 SharedPreferences preferences = getSharedPreferences("pref", 0); 我得到错误 Cannot make a static reference to the non-static method getSharedPreferences(String, int) from the type ContextWrapper 我尝试遵循这些链接,但是没有通过静态方法和 静态SharedPreferences访问SharedPreferences的运气。谢谢您的解决方案。

10
Android工具栏为不同片段添加菜单项
我有一个工具栏以及一个导航抽屉。启动应用程序时,将创建工具栏和导航抽屉。当我单击导航抽屉中的项目时,它将启动新的片段并保持相同的工具栏。在启动特定片段时,基本上如何在菜单栏上添加菜单项,例如搜索,添加,编辑?我不希望它们在程序开始时而是动态创建的。另外,我如何能够单击这些按钮并使它们启动其他片段。我希望在一个片段中,与另一个片段中的编辑按钮相比,工具栏中的编辑按钮具有特定的作用。谢谢! 菜单工具栏: <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/edit" android:orderInCategory="1" android:title="Edit" app:showAsAction="always" android:icon="@drawable/pencil_icon"/> <item android:id="@+id/add" android:orderInCategory="1" android:title="Add" app:showAsAction="always" android:icon="@drawable/plus_icon"/> 工具列: <?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="55dp" android:background="#10a1ff" android:title="Home" />

9
带有片段的导航栏
我有一个标签式动作条/ viewpager布局三个标签说一,乙,和Ç。在标签Ç标签(片段),我加入另一片段发言权片段d。与 DFragment f= new DFragment(); ft.add(android.R.id.content, f, ""); ft.remove(CFragment.this); ft.addToBackStack(null); ft.commit(); 我在DFragment的onResume中修改操作栏以添加按钮: ActionBar ab = getActivity().getActionBar(); ab.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); ab.setDisplayHomeAsUpEnabled(true); ab.setDisplayShowHomeEnabled(true); 现在在DFragment中,当我按硬件(电话)“后退”按钮时,我返回到原始的Tabbed(ABC)布局,并选择了CFragment。如何通过操作栏上按钮实现此功能?

2
方向变化时处理片段的简便方法
public class MainActivity extends Activity implements MainMenuFragment.OnMainMenuItemSelectedListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager .beginTransaction(); // add menu fragment MainMenuFragment myFragment = new MainMenuFragment(); fragmentTransaction.add(R.id.menu_fragment, myFragment); //add content DetailPart1 content1= new DetailPart1 (); fragmentTransaction.add(R.id.content_fragment, content1); fragmentTransaction.commit(); } public void onMainMenuSelected(String …

12
Android片段。在屏幕旋转或配置更改期间保留AsyncTask
我正在开发一个智能手机/平板电脑应用程序,仅使用一个APK,并根据屏幕大小加载所需的资源,最好的设计选择似乎是通过ACL使用片段。 到目前为止,此应用程序一直运行良好,仅基于活动。这是一个模拟类,它说明了我如何处理“活动”中的AsyncTasks和ProgressDialogs以便即使在屏幕旋转或通信过程中发生配置更改时也能正常工作的情况。 我不会更改清单以避免活动的重演,我有很多理由不愿意这样做,但主要是因为官方文档说不建议这样做,而且到目前为止,我已经设法不使用它了,所以请不要建议路线。 public class Login extends Activity { static ProgressDialog pd; AsyncTask<String, Void, Boolean> asyncLoginThread; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.login); //SETUP UI OBJECTS restoreAsyncTask(); } @Override public Object onRetainNonConfigurationInstance() { if (pd != null) pd.dismiss(); if (asyncLoginThread != null) return (asyncLoginThread); return super.onRetainNonConfigurationInstance(); } private void …

8
XML中的Android onClick与OnClickListener
我意识到以前有人问过类似措词的问题,但这是不同的。我在开发android应用程序时还很陌生,关于android:onclick=""XML属性和setOnClickListener方法之间的区别,我有三个问题。 两者之间有什么区别?在编译时或运行时或在两者时发现两种实现之间的区别吗? 哪些用例有利于哪种实现? Android中使用片段对实现选择有何不同?

12
如何使用后退按钮之类的按钮关闭当前片段?
我尝试使用Imagebutton关闭当前片段。 我在Fragment-A中,当我单击按钮时它将变为Fragment-B。 当我单击“片段B”处的按钮时,它将转到“片段C”并关闭“片段B”。 如果单击Fragment-C的后退按钮,它将返回到Fragment-A。 我尝试的代码如下所示 camera_album = (ImageButton) view.findViewById(R.id.camera_album); camera_album.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { closefragment(); Fragment fragment = FileBrowserFragment.newInstance(null, null, null) ; MainActivity.addFragment(LocalFileBrowserFragment.this, fragment) ; } }); private void closefragment() { getActivity().getFragmentManager().beginTransaction().remove(this).commit(); } 当我单击片段B处的后退按钮时,它变为片段C。 但是,当我单击Fragment-C上的后退按钮时,它不会返回到Fragment-A。它回到空的背景。如果要返回至Fragment-A,则必须再次单击“后退”按钮。 所以,似乎没有关闭当前片段完整。 如何完成当前片段,如Android的“后退”按钮?

4
Android-片段事务上的自定义动画未运行
我正在使用带有支持包v4的Google API 8(Android 2.2)。 它不会产生任何错误或动画。 交易: FragmentTransaction transaction = manager.beginTransaction(); transaction.replace(R.id.content, myFragment); transaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right); transaction.commit(); 动画: slide_in_left.xml <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="700" android:fromXDelta="-100%" android:toXDelta="0%" > </translate> </set> slide_out_right.xml <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="700" android:fromXDelta="0%" android:toXDelta="100%" > </translate> </set> 有人知道这里发生了什么吗?

14
如何从片段内部隐藏软键盘?
我有一个FragmentActivity使用AViewPager服务几个片段。每个都是ListFragment具有以下布局的: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="8dp"> <ListView android:id="@id/android:list" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <EditText android:id="@+id/entertext" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout> 开始活动时,将显示软键盘。为了解决这个问题,我在片段中做了以下操作: @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //Save the container view so we can access the window token viewContainer = container; …

17
从onActivityResult显示DialogFragment
我的onActivityResult中有以下代码用于我的片段: onActivityResult(int requestCode, int resultCode, Intent data){ //other code ProgressFragment progFragment = new ProgressFragment(); progFragment.show(getActivity().getSupportFragmentManager(), PROG_DIALOG_TAG); // other code } 但是,出现以下错误: Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState 有人知道怎么回事,或者我怎么解决这个问题?我应该注意我正在使用Android支持包。

15
Android Activity ClassNotFoundException-尝试了一切
我刚刚将应用程序重构为框架库和应用程序,但是现在当我尝试在模拟器中启动该应用程序时,我得到了以下错误堆栈跟踪: 06-02 18:22:35.529: E/AndroidRuntime(586): FATAL EXCEPTION: main 06-02 18:22:35.529: E/AndroidRuntime(586): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.matthewrathbone.eastersays/com.matthewrathbone.eastersays.EasterSimonSaysActivity}: java.lang.ClassNotFoundException: com.matthewrathbone.eastersays.EasterSimonSaysActivity in loader dalvik.system.PathClassLoader[/data/app/com.matthewrathbone.eastersays-1.apk] 06-02 18:22:35.529: E/AndroidRuntime(586): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585) 06-02 18:22:35.529: E/AndroidRuntime(586): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 06-02 18:22:35.529: E/AndroidRuntime(586): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 06-02 18:22:35.529: E/AndroidRuntime(586): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 06-02 18:22:35.529: E/AndroidRuntime(586): at android.os.Handler.dispatchMessage(Handler.java:99) 06-02 18:22:35.529: E/AndroidRuntime(586): at …

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.