
] 1 ] 1我使用了android设计支持tablayout,我在选项卡上都有图标和文本..我使用customtabview垂直对齐图标和文本..我想在选择/取消选择时更改图标和文本的颜色.
我为不同的选项卡图标制作了选择器文件.但事情是当我运行项目时,第一个默认选项卡未被选中我希望它们默认被选中..如果我转到其他选项卡然后再刷回第一个选项卡它工作得很好..主要的问题是我不能让第一个选项卡片段被选中..请帮助我,因为我坚持过去2天..我不想使用任何来自github的库
MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
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 java.util.ArrayList;
import java.util.List;
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.LayoutInflater;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private int[] tabIcons = {
R.drawable.image1, …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用设计支持库来实现选项卡.问题是当我删除属性时,viewpager与工具栏和tablayout重叠
app:layout_behavior="@string/appbar_scrolling_view_behavior"
Run Code Online (Sandbox Code Playgroud)
viewpager.
当我添加属性时,viewpager溢出屏幕.
我只需要viewpager占用tablayout和工具栏后面留下的空间而不是溢出屏幕......
这是我的activity_main.xml
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
这是我的fragment_1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<TextView
android:id="@+id/tV_item1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_horizontal_margin"
android:gravity="center"
android:text="@string/tab1_q_persons"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="0 people"
android:textSize="30sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/iV_plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> …Run Code Online (Sandbox Code Playgroud) android android-layout android-fragments android-viewpager androiddesignsupport
我Floating Action Button使用以下代码更改了backgroundTintList颜色:
fab.setBackgroundTintList(ColorStateList.valueOf(mResources.getColor(R.color.fab_color)));
Run Code Online (Sandbox Code Playgroud)
但我最终在API 4.4.2上获得以下内容:
API 21上的一切看起来都很好<=但是除了API 21以外的任何东西,我对FAB都有这个问题.
我是以编程方式创建FAB,如下所示:
FloatingActionButton fab = new FloatingActionButton(this);
CoordinatorLayout.LayoutParams layoutParams = new CoordinatorLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
fab.setLayoutParams(layoutParams);
layoutParams.rightMargin = mResources.getDimensionPixelSize(R.dimen.activity_horizontal_margin);
((CoordinatorLayout) findViewById(R.id.coordinatorLayout)).addView(fab);
CoordinatorLayout.LayoutParams p = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
p.setAnchorId(R.id.appBarLayout);
p.anchorGravity = Gravity.BOTTOM | Gravity.END;
fab.setLayoutParams(p);
fab.setVisibility(View.VISIBLE);
fab.setBackgroundTintList(ColorStateList.valueOf(mResources.getColor(R.color.fab_color)));
fab.setImageDrawable(getResources().getDrawable(R.drawable.ic_button));
Run Code Online (Sandbox Code Playgroud)
我也正好由官方来运行源代码的FloatingActionButton,我看到他们在这里实例化一个borderDrawable:
@Override
void setBackgroundDrawable(Drawable originalBackground, ColorStateList backgroundTint,
PorterDuff.Mode backgroundTintMode, int rippleColor, int borderWidth) {
// Now we need to tint the original background with the tint
mShapeDrawable = DrawableCompat.wrap(originalBackground.mutate());
DrawableCompat.setTintList(mShapeDrawable, backgroundTint); …Run Code Online (Sandbox Code Playgroud) 我有一个viewpager,它放在一个协调器布局中.在viewpager的特定片段中,我有一个回收器视图和片段底部的视图.问题是最初片段底部的视图是隐藏的,只有当我向上滚动时才可见.我希望视图保持在屏幕的底部.
文件名:activity_main.xml
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
style="@style/ToolBarStyle"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:layout_alignParentTop="true"
android:background="@color/toolbarColor"
android:contentInsetEnd="16dp"
android:contentInsetRight="16dp"
android:minHeight="?actionBarSize"
android:paddingBottom="0dp"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
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"
app:layout_behavior="com.dekorate.android.Utils.AppBarLayoutBehavior" />
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud) 我的应用程序上有三个活动.MainActivity的布局是DrawerLayout fitsSystemWindows="true".另外两个活动的根元素是具有相同属性的CoordinatorLayout.我已经android:launchMode="singleTop"为清单中的所有活动定义了.
所有活动都使用相同的主题AppTheme,该主题具有以下属性:
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:navigationBarColor">@color/colorPrimaryDark</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowBackground">@color/background</item>
<item name="android:colorBackground">@color/background</item>
</style>
Run Code Online (Sandbox Code Playgroud)
这适用于MainActivity,但不适用于状态栏为黑色的其他两个(而不是应该是透明的).支持库的先前版本(v7:27.0.2)不是这种情况.任何人都知道这里可能有什么问题吗?提前致谢!
我注意到TabLayout它只Fragments需要显示一次,所以当我替换Fragment包含TabLayout并切换回它时,它不会重新创建Fragments它保持,因此它们不再显示任何内容.是这样的设计还是我做错了什么?我Fragment在a中使用外部NavigationDrawer,当用户切换到另一个Fragment并返回时,该选项卡Fragments为空,因为它们不会被重新创建.我还注意到在切换回包含它们的片段后,Tabs的行为非常奇怪(它们在跳跃下面的白线,你可以在两个标签之间保持它而不触摸屏幕)
我正在使用的代码:
public class Fragment1 extends Fragment {
private static ViewPager viewPager;
private static TabLayout tabLayout;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Utils.changeLanguage(getActivity());
final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.LinesOverlay);
LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
View view = localInflater.inflate(R.layout.fragment1, container, false);
viewPager = (ViewPager) view.findViewById(R.id.viewpager);
tabLayout = (TabLayout) view.findViewById(R.id.tabs);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); …Run Code Online (Sandbox Code Playgroud) android android-fragments android-tabs navigation-drawer androiddesignsupport
我TabLayout在我的应用程序中使用了最新的设计支持库.选项卡附加到viewpager,后者为每个选项卡加载片段.我想禁用所有选项卡,直到viewpager为用户选择的选项卡加载片段.我无法禁用tablayout或使其无法点击.我曾经使用过setEnabled(false),setClickable(false)但它没有用.我可以通过使用使其隐藏,setVisiblity(View.GONE)但我希望标签始终可见.
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setTabMode(TabLayout.MODE_FIXED);
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.near_me_hover).setTag(1));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.all_hostels).setTag(2));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.top_five).setTag(3));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.advanced_search).setTag(4));
tabLayout.setEnabled(false);
tabLayout.setClickable(false);
Run Code Online (Sandbox Code Playgroud)
XML
android.support.design.widget.TabLayout
android:id ="@ + id/tabLayout"android:layout_width ="match_parent"
android:layout_height ="0dp"
android:layout_weight ="0.15"
android:scrollbars ="horizontal"
android: splitMotionEvents ="false">
@Override
public void onTabSelected(TabLayout.Tab tab) {
switch (tab.getPosition()) {
case 0:
viewPager.setCurrentItem(tab.getPosition());
tab.setIcon(R.drawable.near_me_hover);
break;
case 1:
viewPager.setCurrentItem(tab.getPosition());
tab.setIcon(R.drawable.all_hostels_hover);
break;
case 2:
viewPager.setCurrentItem(tab.getPosition());
tab.setIcon(R.drawable.top_five_hover);
break;
case 3:
viewPager.setCurrentItem(tab.getPosition());
tab.setIcon(R.drawable.advanced_search_hover);
break;
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
switch (tab.getPosition()) {
case 0:
tab.setIcon(R.drawable.near_me); …Run Code Online (Sandbox Code Playgroud) tabs android android-fragments androiddesignsupport android-tablayout
我用TabLayout创建了一个视图寻呼机.现在问题是,如果我有2个或更多选项卡,那么一切都很好.但如果我只有一个标签,那么它不会覆盖整个屏幕宽度.PFA xml布局
<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed"
style="@style/TabLayout"
android:fillViewport="true"
android:layout_below="@id/ssrToolbar"/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/sliding_tabs"
android:layout_above="@+id/llButtonLayout"
android:background="@android:color/white" />
Run Code Online (Sandbox Code Playgroud)
这里还有两张图片
我试图隐藏并recyclerview在协调器布局的帮助下显示我的滚动视图.
我的视图是一个linearlayout按钮,它不是一个fab,toolbar或者tablayout我已经知道如何在滚动时隐藏它们.
请注意,这不是重复,因为所有答案都显示如何制作工具栏或tablayout
这是我正在使用的xml
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/prodMain"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto">
<RelativeLayout
android:id="@+id/LinearLayout01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/gray_light"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<LinearLayout
android:id="@+id/linearFilterLayout"
android:layout_width="match_parent"
app:layout_behavior="fc.admin.fcexpressadmin.itemdecorators.FABFloatOnScroll"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin10dp"
android:background="@color/white"
android:orientation="horizontal"
android:padding="@dimen/margin10dp"
android:visibility="visible"
android:weightSum="3">
</LinearLayout>
<ViewFlipper
android:id="@+id/ViewFlipper01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/linearFilterLayout"
android:layout_marginBottom="@dimen/margin10dp"
android:layout_marginTop="@dimen/margin6dp"
android:background="@color/gray_light"
android:visibility="visible">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btnFooterRefresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:text="Refresh"
android:visibility="visible"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/gridview"
android:layout_width="match_parent"
android:clipToPadding="false"
android:layout_height="match_parent"
android:layout_above="@+id/btnFooterRefresh"
android:cacheColorHint="@android:color/transparent"
android:listSelector="@android:color/transparent"
android:scrollbars="vertical"
android:scrollingCache="false"
android:visibility="visible"/>
</RelativeLayout> …Run Code Online (Sandbox Code Playgroud) Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mypackage/com.mypackage.activities.HomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.ContentFrameLayout.setDecorPadding(int, int, int, int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2721)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2782)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1509)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6286)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.ContentFrameLayout.setDecorPadding(int, int, int, int)' on a null object reference
at android.support.v7.app.AppCompatDelegateImplV9.applyFixedSizeWindow(AppCompatDelegateImplV9.java:537)
at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:331)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.watchlivetv.onlineradioapp.activities.HomeActivity.onCreate(HomeActivity.java:99)
at android.app.Activity.performCreate(Activity.java:6778)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1121)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2675)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2782) …Run Code Online (Sandbox Code Playgroud) android android-appcompat androiddesignsupport android-framelayout