所以,我创建了一个包含以下内容的片段的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageButton
android:layout_width="match_parent"
android:layout_height="110dp"
app:srcCompat="@drawable/flourbutton"
android:id="@+id/imageButton"
android:background="@android:color/transparent"
android:scaleType="fitXY"
style="?attr/borderlessButtonStyle"/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="110dp"
app:srcCompat="@drawable/sugara"
android:id="@+id/flour"
android:background="@android:color/transparent"
android:scaleType="fitXY"
style="?attr/borderlessButtonStyle"/>
</LinearLayout>
问题是:
我的应用程序中有导航抽屉,这使得它非常滞后......可能是图片......我该怎么办?
现在按钮周围有白色边框,但不在它们之间 如下图所示: buttons problem 我怎样才能使它们适合整个布局的宽度和高度?
谢谢大家!!
Java代码:
package com.example.matancohen.sg50;
import android.content.Intent;
import android.graphics.Camera;
import android.os.Bundle;
import android.os.Handler;
import android.os.MessageQueue;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.FragmentManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
displayView(R.id.nav_home);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
private void displayView(int itemId) {
Fragment fragment = null;
switch (itemId) {
case R.id.nav_home:
fragment = new Article();
break;
case R.id.nav_Recipes:
fragment = new Recipes();
break;
}
if (fragment != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_main, fragment);
ft.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
displayView(item.getItemId());
return true;
}
}
答案 0 :(得分:0)
首先,不要在每个项目选择上初始化DrawerLayout
。在onCreate中执行一次,然后像drawer.closeDrawers();
一样使用它。
其次,我没有在您的片段中看到任何大量数据,但对于您拥有大量数据的片段,您可以使用Runnable
替换片段。所以片段加载了交叉淡入淡出效果,下面是如何实现它。
private void displayView(int itemId) {
Fragment fragment = null;
switch (itemId) {
case R.id.nav_home:
fragment = new Article();
break;
case R.id.nav_Recipes:
fragment = new Recipes();
break;
}
if(fragment != null) {
Runnable mPendingRunnable = new Runnable() {
@Override
public void run() {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(android.R.anim.fade_in,
android.R.anim.fade_out);
fragmentTransaction.replace(R.id.frame, fragment);
fragmentTransaction.commitAllowingStateLoss();
}
};
mHandler.post(mPendingRunnable);
drawer.closeDrawers();
}
}