gpt4 book ai didi

android.support.design.widget.TabLayout 不显示标签

转载 作者:行者123 更新时间:2023-11-29 00:10:33 25 4
gpt4 key购买 nike

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;
}

}

最佳答案

错误在activity_main.xml

android:layout_height="?attr/actionBarSize" <!-- WRONG -->

到目前为止,我找到的最好的修复方法是

android:layout_height="wrap_content" <!-- RIGHT -->

关于android.support.design.widget.TabLayout 不显示标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30561812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com