【问题标题】:Unable to get my Android XML file to Inflate via fragments无法通过片段让我的 Android XML 文件膨胀
【发布时间】:2014-06-09 09:00:39
【问题描述】:

StackOverflowians 好,我对我正在开发的 Android 应用有疑问。我在此链接中使用指南中的教程:http://www.jamesfroggatt.com/2014/04/892/ 虽然片段本身是可见的,但我似乎无法让 xml 正确膨胀。目前,这是我的应用程序屏幕的样子:

问题是,我的包含 Autos 片段的 xml 文件实际上是这样写的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<!-- fragment_autos -->

    <TextView
        android:id="@+id/autos_updated_as_of"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button and whatnot" />



    <ListView
        android:id="@+id/autos_list"
        android:layout_width="match_parent"
        android:layout_height="fill_parent" >
    </ListView>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Words!"
        android:textAppearance="?android:attr/textAppearanceLarge" />


</LinearLayout>

所以应该在汽车标签上显示一些东西,对吧?最后,这是我的 Java 代码(大部分是从前面提到的教程中复制的,有些删减以减少代码的混乱)

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;



public class NewMainActivity extends Activity {

    private static final String TAB_KEY_INDEX = "tab_key";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // ActionBar
        ActionBar actionbar = getActionBar();
        actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // create new tabs and set up the titles of the tabs
        ActionBar.Tab mAutoTab = actionbar.newTab().setText("Autos");
        ActionBar.Tab mEarningsTab = actionbar.newTab().setText("Earnings");


        // create the fragments
        Fragment mAutosFragment = new AutosFragment();
        Fragment mEarningsFragment = new EarningsFragment();

        // bind the fragments to the tabs - set up tabListeners for each tab
        mAutoTab.setTabListener(new MyTabsListener(mAutosFragment, getApplicationContext()));
        mEarningsTab.setTabListener(new MyTabsListener(mEarningsFragment, getApplicationContext()));

        // add the tabs to the action bar
        actionbar.addTab(mAutoTab);
        actionbar.addTab(mEarningsTab);

        if (savedInstanceState != null) {
            Toast.makeText(getApplicationContext(),
                    "tab is " + savedInstanceState.getInt(TAB_KEY_INDEX, 0),
                    Toast.LENGTH_SHORT).show();

            actionbar.setSelectedNavigationItem(savedInstanceState.getInt(
                    TAB_KEY_INDEX, 0));
        }
    } 

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
                MenuInflater inflater = getMenuInflater();
                inflater.inflate(R.menu.tabs_menu, menu);
                return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
                switch (item.getItemId()) {
                case R.id.menuitem_search:
                    Toast.makeText(this, "Search", Toast.LENGTH_LONG).show();
                    return true;

                }
                return false;
    }


    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Toast.makeText(
                this,
                "onSaveInstanceState: tab is"
                        + getActionBar().getSelectedNavigationIndex(),
                Toast.LENGTH_SHORT).show();
        outState.putInt(TAB_KEY_INDEX, getActionBar()
                .getSelectedNavigationIndex());

    }


    class MyTabsListener implements ActionBar.TabListener {
        public Fragment fragment;
        public Context context;

        public MyTabsListener(Fragment fragment, Context context) {
            this.fragment = fragment;
            this.context = context;

        }

        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            Toast.makeText(context, "Reselected!", Toast.LENGTH_SHORT).show();

        }

        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            Toast.makeText(context, "Selected!", Toast.LENGTH_SHORT).show();
            ft.replace(R.id.fragment_container, fragment);
        }

        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            Toast.makeText(context, "Unselected!", Toast.LENGTH_SHORT).show();
            ft.remove(fragment);
        }
    }
}

-还有 AutosFragment Java 文件-

//Handles the Autos Fragment of the tabs from the main menu
public class AutosFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_autos, container, false);
    }

}

任何人都知道为什么 xml 没有正确地与按钮、文本视图和列表一起膨胀?我有点迷茫!

【问题讨论】:

    标签: android xml android-activity android-fragments android-inflate


    【解决方案1】:

    试着摆脱这个:

    if (savedInstanceState != null) {
            Toast.makeText(getApplicationContext(),
                    "tab is " + savedInstanceState.getInt(TAB_KEY_INDEX, 0),
                    Toast.LENGTH_SHORT).show();
    
            actionbar.setSelectedNavigationItem(savedInstanceState.getInt(
                    TAB_KEY_INDEX, 0));
        }
    

    此外,由于您在 fragment_auto 布局中有按钮和文本视图,请尝试将其更改为(另外):

    setContentView(R.layout.fragment_auto);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多