当第一个片段是地图时如何更改片段 activity
how to change fragments when the first fragment is a map activity
我有一张地图 activity,其中还包含一个导航抽屉布局,抽屉中有项目。我希望能够单击抽屉中的一个项目,然后我选择的项目将打开 activity。我尝试使用 startActivity 在单独的页面中调用 class,但它扭曲了我模拟器中的 xml 文件。我也试过 updateFragment();但它显示我的新 class 是错误的。有人知道我能做什么吗?
这是我的 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>
这是预览中的样子;
这是它在模拟器上的样子:(
一张图片:
这是因为您为每个图像视图使用了 layout_width="100dp" 和 20dp 的边距,因此布局的最终宽度大于设备的屏幕宽度。例如,如果您将预览从 Nexus 6 更改为 Nexus One,就可以看到它。
您可以为每个图像或 3 列的 TableLayout 使用 layout_weight 参数,或者您可以尝试使用 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>
我有一张地图 activity,其中还包含一个导航抽屉布局,抽屉中有项目。我希望能够单击抽屉中的一个项目,然后我选择的项目将打开 activity。我尝试使用 startActivity 在单独的页面中调用 class,但它扭曲了我模拟器中的 xml 文件。我也试过 updateFragment();但它显示我的新 class 是错误的。有人知道我能做什么吗? 这是我的 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>
这是预览中的样子;
这是它在模拟器上的样子:(
一张图片:
这是因为您为每个图像视图使用了 layout_width="100dp" 和 20dp 的边距,因此布局的最终宽度大于设备的屏幕宽度。例如,如果您将预览从 Nexus 6 更改为 Nexus One,就可以看到它。
您可以为每个图像或 3 列的 TableLayout 使用 layout_weight 参数,或者您可以尝试使用 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>