当第一个片段是地图活动时如何更改片段

时间:2015-08-31 21:30:38

标签: android android-fragments google-maps-api-3 navigation-drawer android-fragmentactivity

我有一个地图活动,其中还包含导航抽屉布局,其中包含抽屉中的项目。我希望能够单击抽屉中的项目,我选择的项目将打开一个活动。我尝试使用startActivity在单独的页面中调用该类,但它在我的模拟器中扭曲了我的xml文件。我也试过updateFragment();但它显示我的新课程是一个错误。谁会知道我能做什么? 这是我的MapActivity.java文件

public class MapsActivity extends FragmentActivity {

private GoogleMap mMap; 
DrawerLayout drawerLayout;
ListView listView;
ActionBarDrawerToggle drawerToggle;
String[]items;
int selectedPosition=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    setUpMapIfNeeded();

    items=getResources().getStringArray(R.array.options);
    drawerLayout=(DrawerLayout)findViewById(R.id.drawer_layout);
    listView=(ListView)findViewById(R.id.drawer_list);
    ArrayAdapter<String>adapter=new ArrayAdapter<String>(MapsActivity.this,R.layout.drawer_list_item,items);
    listView.setAdapter(adapter);
    drawerToggle=new ActionBarDrawerToggle(MapsActivity.this,drawerLayout,R.drawable.images,R.string.drawer_open,R.string.drawer_close){
        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
        }
    };
    drawerLayout.setDrawerListener(drawerToggle);
   // getActionBar().setHomeButtonEnabled(false);
  //  getActionBar().setDisplayHomeAsUpEnabled(false);
  //  getActionBar().setDisplayShowTitleEnabled(false);
   // getActionBar().setDisplayShowHomeEnabled(false);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectedPosition = position;
            if (selectedPosition==2){
            startActivity(new Intent(MapsActivity.this, MenuActivity.class);

            }

           // updateFragment();
            drawerLayout.closeDrawer(listView);
        }
    });
   // selectedPosition=0;
   // updateFragment();
}

@Override
protected void onResume() {
    super.onResume();
    setUpMapIfNeeded();
}

private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            setUpMap();
        }
    }
}

private void setUpMap() {
    mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
   private void updateFragment() {
    FragmentManager fragmentManager=getFragmentManager();
    WebViewFragment rFragment=new WebViewFragment();

    Bundle data=new Bundle();
    data.putString("title",items[selectedPosition]);
    rFragment.setArguments(data);
    FragmentTransaction ft=fragmentManager.beginTransaction();
    ft.replace(R.id.content_frame,rFragment);
    ft.commit();



}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    drawerToggle.syncState();
}
}

我的MenuActivity.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.xxxxxx.qdoba.MenuFragment">

<!-- TODO: Update blank fragment layout -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="@drawable/woodcrop">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="MENU"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textStyle="bold"
        android:textSize="60dp"
        android:textColor="#ff912424" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="140dp"
            android:layout_marginTop="100dp">

            <ImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:id="@+id/imageView"
                android:layout_marginTop="20dp"
                android:layout_marginLeft="20dp" />

            <ImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:id="@+id/imageView2" 
                android:layout_marginTop="20dp"
                android:layout_marginLeft="30dp" />

            <ImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:id="@+id/imageView3"
                android:layout_marginTop="20dp"
                android:layout_marginLeft="30dp" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="140dp"
            android:layout_gravity="center_horizontal">
            <ImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:id="@+id/imageView4"
                android:layout_marginTop="20dp"
                android:layout_marginLeft="20dp" />

            <ImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:id="@+id/imageView5"
                android:layout_marginTop="20dp"
                android:layout_marginLeft="30dp" />

            <ImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:id="@+id/imageView6"
                android:layout_marginTop="20dp"
                android:layout_marginLeft="30dp" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="140dp"
            android:layout_gravity="center_horizontal">
            <ImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:id="@+id/imageView7"
                android:layout_marginTop="20dp"
                android:layout_marginLeft="20dp" />

            <ImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:id="@+id/imageView8"
                android:layout_marginTop="20dp"
                android:layout_marginLeft="30dp" />

            <ImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:id="@+id/imageView9"
                android:layout_marginTop="20dp"
                android:layout_marginLeft="30dp" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="140dp"
            android:layout_gravity="center_horizontal">
            <ImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:id="@+id/imageView10"
                android:layout_marginTop="20dp"
                android:layout_marginLeft="20dp" />

            <ImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:id="@+id/imageView11"
                android:layout_marginTop="20dp"
                android:layout_marginLeft="30dp" />

            <ImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:id="@+id/imageView12"
                android:layout_marginTop="20dp"
                android:layout_marginLeft="30dp" />
        </LinearLayout>
    </LinearLayout>

</RelativeLayout>

这是它在预览中的外观; enter image description here

这是它在模拟器上的外观:(

enter image description here

一张图片: enter image description here

1 个答案:

答案 0 :(得分:1)

之所以发生这种情况,是因为您对每个图像视图和20dp边距使用layout_width =“100dp”,因此布局的最终​​宽度大于设备的屏幕宽度。例如,如果您将预览从Nexus 6更改为Nexus One,则可以看到它 您可以为每个图像使用layout_weight参数,或者为3列使用TableLayout,或者您可以尝试使用GridLayout。
试试这个,例如。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:layout_weight="1"
        android:minHeight="50dp"
        />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:layout_weight="1"
        android:minHeight="50dp"
        />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:layout_weight="1"
        android:minHeight="50dp"
        />
</LinearLayout>

或效果相同

<TableLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/textView"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:stretchColumns="*">

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:id="@+id/imageView"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_margin="10dp"
                    android:layout_weight="1"
                    android:minHeight="50dp"
                    />

                <ImageView
                    android:id="@+id/imageView2"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_margin="10dp"
                    android:layout_weight="1"
                    android:minHeight="50dp"
                    />

                <ImageView
                    android:id="@+id/imageView3"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_margin="10dp"
                    android:layout_weight="1"
                    android:minHeight="50dp"
                    />
            </TableRow>
//your other rows
</TableLayout>