android 封装好的BottomTabBar

 1 在android app开发过程中,会发现很多App的底部(顶部一样) 会仿效IPHONE的设计。。做一个导航。  如下图黑色部分:

                                  

                                 (这个是实现效果)                                                                 这个是设计原型


        1.1 要是中间部分没有那个拱起来的部分,那么可能设计还简单一些。。 仔细分析下设计原型,,,中间那个 摇摇 的图标要大一些,而且和其他图标没在一个水平线上。


      1.2  总的思想就是 先封装一个 没有 凸起 的通用,自动适配的组建。。。然后。。。在上面 通过 FrameLayout 放置一个 突起的组建。。。


       1.3 设计细节

          1.3.1  自己写一个  BottomTabBar   (extends) LinearLayout .  通过 Inflater 加载一个 自定义的 View (通过xml配置), 在View 的xml 中,注意书写好适配就OK了。

                   注意 layout_weight 的使用。(参见上一篇 listview)

      


           自定义LinearLayout:

          

package com.dp.android.widget;

import com.dp.android.elong.shake.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

public class BottomTabBar extends LinearLayout {
	//--implements two kinds of constructions--
	public  BottomTabBar(Context context) {
		super(context);
		
		init(context);
	}


	public BottomTabBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		init(context);
	}
	
	private void init(Context context) {
		LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
		setLayoutParams(lp);
		
		LayoutInflater mInflater = LayoutInflater.from(context);
		
		View v = mInflater.inflate(R.layout.bottom_tabs, null);
		
		addView(v,lp);
	}
}

    

           自定义的View(xml 形式)

<?xml version="1.0" encoding="utf-8"?>
	<LinearLayout 
	   xmlns:android="http://schemas.android.com/apk/res/android" 
	    android:layout_width="fill_parent"
	    android:layout_height="fill_parent"
	    >
	     
	    <!-- 搜索 -->
		<LinearLayout  
			android:id="@+id/ll_bottom_tabls_search" 
			style = "@style/bottom_tabs_ll"
			>
			
		    <ImageView 
		        android:id="@+id/im_search"
				style = "@style/bottom_tabs_image"
				android:src="@drawable/bottom_tab_search" />
		    
		    
			<TextView 
				style = "@style/bottom_tabs_tv"
				android:text="@string/bottom_tab_search"
				/>	    
		    
		 </LinearLayout>     
		 
		<!-- 摇过 -->
		<LinearLayout 
			android:id="@+id/ll_bottom_tabls_history" 
			style = "@style/bottom_tabs_ll"
			>
	
		    <ImageView 
		        android:id="@+id/im_history"
				style = "@style/bottom_tabs_image"
				android:src="@drawable/bottom_tab_history" />
		    
			<TextView 
				style = "@style/bottom_tabs_tv"
				android:text="@string/bootom_tab_shake"
				/>		    
		    		
		 </LinearLayout>
		 
		<!-- 换一个 -->
		<LinearLayout 
			android:id="@+id/ll_bottom_tabls_change" 
			style = "@style/bottom_tabs_ll"
			>
			<ImageView 
			    android:id="@+id/im_change"
			    style = "@style/bottom_tabs_image"
			    android:src="@drawable/bottom_tab_blank"
			    />
			
			<TextView 
			    style = "@style/bottom_tabs_tv"
			    android:text="@string/bottom_tab_change"
			    />
		 </LinearLayout>
		 
		<!-- 收藏 -->
		<LinearLayout 
			android:id="@+id/ll_bottom_tabls_collection" 
			style = "@style/bottom_tabs_ll"
			>
			
			<ImageView
			    android:id="@+id/im_collection"
			    style = "@style/bottom_tabs_image"
			    android:src="@drawable/bottom_tab_collection"
			    />    
			    
			<TextView 
				style = "@style/bottom_tabs_tv"
				android:text="@string/bootom_tab_collection"
				/>	
		 </LinearLayout>	
		 
		<!-- 更多 -->	
		<LinearLayout 
			android:id="@+id/ll_bottom_tabls_more" 
			style = "@style/bottom_tabs_ll"
			>
			
		    <ImageView
		        android:id="@+id/im_more"
		        style = "@style/bottom_tabs_image"
		        android:src="@drawable/bottom_tab_more"
		        />
	
			<TextView 
				style = "@style/bottom_tabs_tv"
				android:text="@string/bootom_tab_more"
				/>		    
		 </LinearLayout>
	
	</LinearLayout>

 

         view 中用到的 三个 style

        

    <style name="bottom_tabs_ll" >
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:background">@color/black</item>
        <item name="android:orientation">vertical</item>
        <item name="android:gravity">center</item>
        <item name="android:layout_gravity">bottom</item>
        <item name="android:layout_weight">1</item>
    </style>
    
    <style name="bottom_tabs_image" >
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:paddingTop">5dip</item>
    </style>
    
    <style name="bottom_tabs_tv" >
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textSize">12sp</item>
        <item name="android:textColor">@color/common_white</item>
        <item name="android:paddingTop">3dip</item>
        <item name="android:gravity">center</item>
    </style>


//---------------------------------------------------------------下面是如何使用自己定义的 组建了-------------------------------------

1: 注意 BottomTabBar ,,, 这里面还用到了自定义的 “凸起” 组件。 MyBottmArc

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/shake_yellow_bg"
        android:orientation="vertical"
        >
        <com.dp.android.widget.BottomTabBar
            android:id="@+id/bottom_tab_bar1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingTop="30dip"
            />  
            
        <com.dp.android.widget.MyBottmArc
            android:id="@+id/im_change"
            android:layout_width="80dip"
            android:layout_height="60dip"
            android:padding="15dip"
            android:src="@drawable/bottom_tab_search"
            android:layout_gravity="top|center_horizontal"
            />         
        
    </FrameLayout>


//--------------------------------------------------------------------------------------下面看看  凸起 是怎么弄的---------------------------------------------------------------------------------

  凸起 也是自己定义了一个 ImageView,  这个ImageView 的背景  就是绘制半个 圆弧。。。“拱形”


package com.dp.android.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.ImageView;

public class MyBottmArc extends ImageView {

	private static Paint paint = new Paint();
	
	public MyBottmArc(Context context) {
		super(context);
	}

	public MyBottmArc(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	public MyBottmArc(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}
	
    protected void onDraw(Canvas canvas) {
    	Rect m_rect = canvas.getClipBounds();
    	canvas.save();
    	
    	paint.setColor(0xff000000);
    	
    	float m_left = m_rect.left;
    	float m_top = m_rect.top;
    	float m_right = m_rect.right;
    	float m_bottom = (1.2f)*(m_rect.bottom);
    	
    	RectF m_rectf = new RectF(m_left,m_top,m_right,m_bottom);
    	canvas.drawOval(m_rectf, paint);
    	
    	canvas.restore();
		
		super.onDraw(canvas);
    }
}



  

       



 
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Retrofit是一个非常流行的网络请求库,它可以帮助我们快速地完成网络请求的操作。在Android中使用Retrofit时,一般会对其进行封装,以方便我们的使用。下面是一个简单的封装示例: 1. 定义接口 首先需要定义一个接口来描述我们的网络请求操作,可以在这个接口中定义多个方法,每个方法对应一个具体的网络请求。 ```java public interface ApiService { @GET("api/user") Call<User> getUserInfo(@Query("userId") int userId); } ``` 2. 创建Retrofit实例 在进行网络请求之前,需要先创建Retrofit实例,代码如下: ```java Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://www.example.com/") .addConverterFactory(GsonConverterFactory.create()) .build(); ``` 其中,baseUrl指定了网络请求的基础URL,addConverterFactory指定了数据解析器,这里使用了GsonConverterFactory。 3. 创建接口实例 接下来,需要创建ApiService的实例: ```java ApiService apiService = retrofit.create(ApiService.class); ``` 4. 发起网络请求 最后,就可以通过ApiService的方法来发起网络请求了: ```java Call<User> call = apiService.getUserInfo(123); call.enqueue(new Callback<User>() { @Override public void onResponse(Call<User> call, Response<User> response) { // 处理响应结果 } @Override public void onFailure(Call<User> call, Throwable t) { // 处理请求失败的情况 } }); ``` 以上就是一个简单的Retrofit封装的示例,当然实际情况中还需要考虑一些其他的因素,比如错误处理、请求取消等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值