的build.gradle
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:design:22.2.0'
compile 'com.android.support:support-v4:22.2.0'
}
activity_main.xml中
<RelativeLayout
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=".MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/abl_top"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary" />
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/abl_top" />
</RelativeLayout>
MainActivity.java
import android.os.Bundle;
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.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
TabLayout tl = (TabLayout) findViewById(R.id.tab_layout);
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
//tl.addTab(tl.newTab().setText("TAB 1"));
//tl.addTab(tl.newTab().setText("TAB 2"));
viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
tl.setupWithViewPager(viewPager);
}
public class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) { super(fm); }
@Override
public Fragment getItem(int position) {
switch (position) {
case 0: return new FragmentOne();
case 1: return new FragmentOne();
default: return null;
}
}
@Override
public int getCount() { return 2; }
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0: return "First Tab";
case 1: return "Second Tab";
default: return "not found";
}
}
}
}
FragmentOne.java
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class FragmentOne extends Fragment {
private final String TAG = getClass().getSimpleName();
private static final String mUrl = "http://goanuj.freeshell.org/txt2lrn/bollywood/box_office.json";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(TAG, "[onCreateView]");
final View v = inflater.inflate(R.layout.fragment_one, container, false);
// setup listview
ListView lv = (ListView) v.findViewById(R.id.lv_items);
// Defined Array values to show in ListView
String[] values = new String[] { "apple", "banana", "cherry", "dog", "earth",
"farther", "grape", "horseradish", "ice cream", "janitor", "kangaroo","lemon"
};
ArrayAdapter adapter = new ArrayAdapter(v.getContext(),
android.R.layout.simple_list_item_1, android.R.id.text1, values);
lv.setAdapter(adapter);
return v;
}
}
答案 0 :(得分:2)
错误在activity_main.xml
android:layout_height="?attr/actionBarSize" <!-- WRONG -->
到目前为止我找到修复它的最佳方法是
android:layout_height="wrap_content" <!-- RIGHT -->
答案 1 :(得分:0)
我将TabLayout放在xml文件的底部解决了这个问题。 它使得Tablayout也可以在preLollipop设备中看到。