gpt4 book ai didi

java - 更改 ViewPager v4 中的背景选项卡

转载 作者:行者123 更新时间:2023-12-01 12:12:39 25 4
gpt4 key购买 nike

我正在尝试更改选项卡 View 页面的颜色,但我已经尝试了所有方法,但没有更改颜色。这是我尝试自定义选项卡的 Activity :LocationsActivity.java:

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.Tab;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;

import com.jad.pholoc.util.LocationsFragmentPagerAdapter;


public class LocationsActivity extends ActionBarActivity {

private ViewPager mPager;
public static FragmentManager fragmentManager;
private LocationsFragmentPagerAdapter fragmentPagerAdapter;
private ActionBar mActionbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_locations);

mActionbar = getSupportActionBar();

mActionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

mPager = (ViewPager) findViewById(R.id.pager);

fragmentManager = getSupportFragmentManager();

ViewPager.SimpleOnPageChangeListener pageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
mActionbar.setSelectedNavigationItem(position);
}
};

mPager.setOnPageChangeListener(pageChangeListener);


fragmentPagerAdapter = new LocationsFragmentPagerAdapter(
fragmentManager);

mPager.setAdapter(fragmentPagerAdapter);

mActionbar.setDisplayShowTitleEnabled(true);

ActionBar.TabListener tabListener = new ActionBar.TabListener() {

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
};

Tab tab = mActionbar.newTab().setText("Lista")
.setTabListener(tabListener);

mActionbar.addTab(tab);

tab = mActionbar.newTab().setText("Mapa").setTabListener(tabListener);

mActionbar.addTab(tab);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.locations, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.acercade) {
Intent i = new Intent(this, AcercaDeActivity.class);
startActivity(i);
return true;
}
return super.onOptionsItemSelected(item);
}

}

activity_locations.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" >

<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>

</RelativeLayout>

样式.xml:

<resources>
<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light">
<item name="actionBarStyle">@style/ActionBarStyle</item>
<item name="actionBarTabStyle">@style/ActionBarTabStyle.Example</item>
</style>

<style name="ActionBarStyle" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="titleTextStyle">@style/TitleBarTextColor</item>
<item name="background">@color/blue_pholoc</item>
<item name="android:backgroundStacked">@color/blue_pholoc</item>
</style>

<style name="TitleBarTextColor" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">@color/white</item>
</style>

<style name="ActionBarTabStyle.Example" parent="@style/Widget.AppCompat.Light.ActionBar.TabView">
<item name="android:background">@drawable/tab_indicator_ab_example</item>
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
</resources>

当前 Activity 的外观是这样的(图片):

https://dl.dropboxusercontent.com/u/30731371/LocationsActivity.png

正如我所说,我想更改选项卡的颜色,但这是不可能的,只是看起来灰色。

PD:我是西类牙人,我的英文写得不好(谷歌翻译)。

最佳答案

您是否考虑过使用这个库:https://github.com/astuetz/PagerSlidingTabStrip

然后您可以在布局中设置如下颜色:

android:background="@color/memrise_blue"

更容易,它还减少了您的代码:)

关于java - 更改 ViewPager v4 中的背景选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27193111/

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