Skip to content

Commit

Permalink
添加bottomBarLayout
Browse files Browse the repository at this point in the history
  • Loading branch information
chaychan committed Jun 23, 2017
1 parent eb9901a commit 022c799
Show file tree
Hide file tree
Showing 18 changed files with 544 additions and 2 deletions.
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
</activity>
<activity android:name=".activity.EllDefaultBottomDemoActivity">
</activity>
<activity android:name=".activity.EllCustomBottomDemoActivity"></activity>
<activity android:name=".activity.EllCustomBottomDemoActivity"/>
<activity android:name=".activity.ExpandableLinearLayoutChooseActivity">
</activity>
<activity android:name=".activity.BottomBarLayoutDemoActivity">
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.chaychan.powerfulviewlibrary.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;

import com.chaychan.powerfulviewlibrary.R;
import com.chaychan.powerfulviewlibrary.fragment.TabFragment;
import com.chaychan.viewlib.bottombarlayout.BottomBarLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class BottomBarLayoutDemoActivity extends FragmentActivity {

@Bind(R.id.vp_content)
ViewPager vpContent;
@Bind(R.id.bbl)
BottomBarLayout bbl;

private List<TabFragment> mFragmentList = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bottom_bar_layout_demo);
ButterKnife.bind(this);

initData();
initListener();
}

private void initData() {

TabFragment homeFragment = new TabFragment();
Bundle bundle1 = new Bundle();
bundle1.putString(TabFragment.CONTENT,"首页");
homeFragment.setArguments(bundle1);
mFragmentList.add(homeFragment);

TabFragment videoFragment = new TabFragment();
Bundle bundle2 = new Bundle();
bundle2.putString(TabFragment.CONTENT,"视频");
videoFragment.setArguments(bundle2);
mFragmentList.add(videoFragment);

TabFragment microFragment = new TabFragment();
Bundle bundle3 = new Bundle();
bundle3.putString(TabFragment.CONTENT,"微头条");
microFragment.setArguments(bundle3);
mFragmentList.add(microFragment);

TabFragment meFragment = new TabFragment();
Bundle bundle4 = new Bundle();
bundle4.putString(TabFragment.CONTENT,"我的");
meFragment.setArguments(bundle4);
mFragmentList.add(meFragment);
}

private void initListener() {
vpContent.setAdapter(new MyAdapter(getSupportFragmentManager()));
bbl.setmViewPager(vpContent);
}

class MyAdapter extends FragmentStatePagerAdapter{

public MyAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}

@Override
public int getCount() {
return mFragmentList.size();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public class MainActivity extends ListActivity {
new DemoBean("ExpandableTextViewDemo",ExpandableTextViewDemoActivity.class),
new DemoBean("PieChartViewDemo",PieChartViewDemoActivity.class),
new DemoBean("RunningTextViewDemo",RunningTextViewDemoActivity.class),
new DemoBean("ExpandableLinearLayoutDemo",ExpandableLinearLayoutChooseActivity.class)
new DemoBean("ExpandableLinearLayoutDemo",ExpandableLinearLayoutChooseActivity.class),
new DemoBean("BottomBarLayoutDemoActivity",BottomBarLayoutDemoActivity.class)
};

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.chaychan.powerfulviewlibrary.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
* @author ChayChan
* @date 2017/6/23 11:22
*/
public class TabFragment extends Fragment {

public static final String CONTENT = "content";
private TextView mTextView;


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle bundle) {
mTextView = new TextView(getActivity());
mTextView.setGravity(Gravity.CENTER);
String content = getArguments().getString(CONTENT);
mTextView.setText(content);
return mTextView;
}


}
82 changes: 82 additions & 0 deletions app/src/main/res/layout/activity_bottom_bar_layout_demo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical"
>

<android.support.v4.view.ViewPager
android:id="@+id/vp_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
></android.support.v4.view.ViewPager>

<com.chaychan.viewlib.bottombarlayout.BottomBarLayout
android:id="@+id/bbl"
android:layout_width="match_parent"
android:layout_height="56dp"
android:orientation="horizontal"
android:gravity="center"
android:layout_gravity="center"
android:background="@color/tab_gb"
>

<com.chaychan.viewlib.bottombarlayout.BottomBarItem
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
app:iconNormal="@mipmap/tab_home_normal"
app:iconSelected="@mipmap/tab_home_selected"
app:itemText="首页"
app:textColorNormal="@color/tab_normal_color"
app:textColorSelected="@color/tab_selected_color"
app:itemTextSize="10sp"
app:itemMarginTop="0dp"
/>

<com.chaychan.viewlib.bottombarlayout.BottomBarItem
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
app:iconNormal="@mipmap/tab_video_normal"
app:iconSelected="@mipmap/tab_video_selected"
app:itemText="视频"
app:textColorNormal="@color/tab_normal_color"
app:textColorSelected="@color/tab_selected_color"
app:itemTextSize="10sp"
app:itemMarginTop="0dp"
/>


<com.chaychan.viewlib.bottombarlayout.BottomBarItem
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
app:iconNormal="@mipmap/tab_micro_normal"
app:iconSelected="@mipmap/tab_micro_selected"
app:itemText="微头条"
app:textColorNormal="@color/tab_normal_color"
app:textColorSelected="@color/tab_selected_color"
app:itemTextSize="10sp"
app:itemMarginTop="0dp"
/>

<com.chaychan.viewlib.bottombarlayout.BottomBarItem
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
app:iconNormal="@mipmap/tab_me_normal"
app:iconSelected="@mipmap/tab_me_selected"
app:itemText="我的"
app:textColorNormal="@color/tab_normal_color"
app:textColorSelected="@color/tab_selected_color"
app:itemTextSize="10sp"
app:itemMarginTop="0dp"
/>

</com.chaychan.viewlib.bottombarlayout.BottomBarLayout>

</LinearLayout>
Binary file added app/src/main/res/mipmap-xxhdpi/tab_home_normal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/mipmap-xxhdpi/tab_home_selected.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/mipmap-xxhdpi/tab_me_normal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/mipmap-xxhdpi/tab_me_selected.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/mipmap-xxhdpi/tab_micro_normal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/mipmap-xxhdpi/tab_video_normal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>

<color name="tab_gb">#F3F5F4</color>
<color name="tab_normal_color">#515051</color>
<color name="tab_selected_color">#D33D3C</color>

</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package com.chaychan.viewlib.bottombarlayout;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chaychan.viewlib.R;
import com.chaychan.viewlib.utils.UIUtils;


/**
* @author ChayChan
* @description: 底部tab条目
* @date 2017/6/23 9:14
*/

public class BottomBarItem extends LinearLayout {

private Context mContext;
private int mIconNormalResourceId;//普通状态图标的资源id
private int mIconSelectedResourceId;//选中状态图标的资源id
private String mText;//文本
private int mTextSize = 12;//文字大小 默认为12sp
private int mTextColorNormal = 0xFF999999; //描述文本的默认显示颜色
private int mTextColorSelected = 0xFF46C01B; //述文本的默认选中显示颜色
private int mMarginTop = 5;//文字和图标的距离,默认5dp
private TextView mTextView;
private ImageView mImageView;

public BottomBarItem(Context context) {
this(context, null);
}

public BottomBarItem(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}

public BottomBarItem(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);

mContext = context;

TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.BottomBarItem);

mIconNormalResourceId = ta.getResourceId(R.styleable.BottomBarItem_iconNormal, -1);
mIconSelectedResourceId = ta.getResourceId(R.styleable.BottomBarItem_iconSelected, -1);

mText = ta.getString(R.styleable.BottomBarItem_itemText);
mTextSize = ta.getDimensionPixelSize(R.styleable.BottomBarItem_itemTextSize, UIUtils.sp2px(mContext,mTextSize));

mTextColorNormal = ta.getColor(R.styleable.BottomBarItem_textColorNormal, mTextColorNormal);
mTextColorSelected = ta.getColor(R.styleable.BottomBarItem_textColorSelected, mTextColorSelected);

mMarginTop = ta.getDimensionPixelSize(R.styleable.BottomBarItem_itemMarginTop, UIUtils.dip2Px(mContext, mMarginTop));
ta.recycle();

checkValues();
init();
}

/**
* 检查传入的值是否完善
*/
private void checkValues() {
if (mIconNormalResourceId == -1) {
throw new IllegalStateException("您还没有设置默认状态下的图标,请指定iconNormal的图标");
}

if (mIconSelectedResourceId == -1) {
throw new IllegalStateException("您还没有设置选中状态下的图标,请指定iconSelected的图标");
}
}

private void init() {
setOrientation(VERTICAL);
setGravity(Gravity.CENTER);

View view = View.inflate(mContext, R.layout.item_bottom_bar, null);
mImageView = (ImageView) view.findViewById(R.id.iv_icon);
mTextView = (TextView) view.findViewById(R.id.tv_text);

mImageView.setImageResource(mIconNormalResourceId);
mTextView.getPaint().setTextSize(mTextSize);
mTextView.setText(mText);
mTextView.setTextColor(mTextColorNormal);

LinearLayout.LayoutParams layoutParams = (LayoutParams) mTextView.getLayoutParams();
layoutParams.topMargin = mMarginTop;
mTextView.setLayoutParams(layoutParams);

addView(view);
}

private ImageView createImageView() {
mImageView = new ImageView(mContext);
mImageView.setImageResource(mIconNormalResourceId);
return mImageView;
}

private TextView creatTextView() {
mTextView = new TextView(mContext);

mTextView.setGravity(Gravity.CENTER);
return mTextView;
}

public void setStatus(boolean isSelected){
mImageView.setImageResource(isSelected?mIconSelectedResourceId:mIconNormalResourceId);
mTextView.setTextColor(isSelected?mTextColorSelected:mTextColorNormal);
}
}
Loading

0 comments on commit 022c799

Please sign in to comment.