在这个布局中,我已经使用片段活动制作了标签布局,并希望显示一些内容,但片段活动中没有显示任何内容。 这是基础xml,其中片段是change.xml
<android.support.v4.widget.DrawerLayout 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:id="@+id/activity_change"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_collapseParallaxMultiplier="0.5"
tools:context="com.example.alpit.formula2.Change">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/change_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/change"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.NoActionBar.PopupOverlay"></android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/change_tab"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:tabGravity="fill">
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/change_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/change_appbar"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</android.support.v4.view.ViewPager>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_change"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/navigation_menu"
app:theme="@style/NavigationTheme">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
这是标签布局的java代码
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Distance extends Fragment {
protected View onCreate(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
return inflater.inflate(R.layout.activity_distance, container, false);
}
}
这是距离的xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_distance"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.alpit.formula2.Distance"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Weight in kg" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Weight in kg" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Weight in kg"
android:textSize="100sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Weight in kg" />
</LinearLayout>
这是我在布局中添加标签的java代码
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
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.view.MenuItem;
import java.util.ArrayList;
import java.util.List;
class Change extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private ActionBarDrawerToggle toggle3;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_change);
Toolbar toolbar = (Toolbar) findViewById(R.id.change);
setSupportActionBar(toolbar);
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.activity_change);
toggle3 = new ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.Close);
drawerLayout.addDrawerListener(toggle3);
toggle3.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_change);
navigationView.setItemIconTintList(null);
navigationView.setNavigationItemSelectedListener(this);
setTitle("Conversion");
viewPager = (ViewPager) findViewById(R.id.change_viewpager);
setUpViewpager(viewPager);
TabLayout tabs = (TabLayout) findViewById(R.id.change_tab);
tabs.setupWithViewPager(viewPager);
}
public boolean onOptionsItemSelected(MenuItem menuItem) {
if (toggle3.onOptionsItemSelected(menuItem)) {
return true;
}
return onOptionsItemSelected(menuItem);
}
private void setUpViewpager(ViewPager upViewpager) {
Adapter_tab adapter = new Adapter_tab(getSupportFragmentManager());
adapter.addFragment(new Distance(), "Distance");
adapter.addFragment(new Mass(), "Mass");
upViewpager.setAdapter(adapter);
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
return false;
}
static class Adapter_tab extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public Adapter_tab(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentTitleList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
答案 0 :(得分:1)
只需在距离
中添加onCreateView即可