如果想让屏幕下方容纳某个组件, 当向上滑动的时候显示出来。可是android 的布局没有支持屏幕底下预留位置。
所以用Scroller实现滑动, 然后动态改变隐藏部分的高度(起始是0)是一个不错的解决方案。
实现步骤
1.自定义View使用Scroller实现滑动。
package com.example.scroller.view;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.Scroller;
public class ScrollCustomView extends LinearLayout {
private Scroller mScroller;
SizeChangeListener listener;
final int DURATION = 500;
final int HEIGHT = 100;
public SizeChangeListener getListener() {
return listener;
}
public void setListener(SizeChangeListener listener) {
this.listener = listener;
}
public ScrollCustomView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
void init(Context context) {
//View.inflate(context, R.layout.layout_bttom, this);
mScroller = new Scroller(context);
}
public void scrollUp() {
mScroller.startScroll(0, 0, 0, HEIGHT, DURATION);
invalidate();
}
public void scrollDown() {
mScroller.startScroll(0, HEIGHT, 0, -HEIGHT, DURATION);
invalidate();
}
@Override
public void computeScroll() {
if (mScroller.computeScrollOffset()) {
int scrollY = mScroller.getCurrY();
scrollTo(mScroller.getCurrX(), scrollY);
if (listener != null)
listener.onShowBottom(scrollY);
invalidate();
}
}
public interface SizeChangeListener {
void onShowBottom(int height);
}
}
2.主Activity, 通过设置滑动时的监听事件, 动态改变底部view的高度。
package com.example.scroller;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import com.example.scroller.view.ScrollCustomView;
import com.example.scroller.view.ScrollCustomView.SizeChangeListener;
public class MainActivity extends Activity implements OnClickListener {
ScrollCustomView customView;
Button button1, button2;
LinearLayout linear2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
customView = (ScrollCustomView) findViewById(R.id.custom_btm);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
linear2 = (LinearLayout) findViewById(R.id.linear2);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
customView.setListener(new SizeChangeListener() {
@Override
public void onShowBottom(int height) {
LayoutParams layoutParams = linear2.getLayoutParams();
layoutParams.height = height;
linear2.setLayoutParams(layoutParams);
}
});
}
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.button1:
customView.scrollUp();
break;
case R.id.button2:
customView.scrollDown();
break;
}
}
}
3. 布局文件如下
<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:background="#ff220000"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/linear1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:text="@string/hello_world" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<LinearLayout
android:id="@+id/linear2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical" >
<include
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="@layout/layout_bttom" />
</LinearLayout>
</FrameLayout>
<com.example.scroller.view.ScrollCustomView
android:id="@+id/custom_btm"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:layout_below="@id/linear1"
android:orientation="vertical" >
<RatingBar
android:id="@+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TimePicker
android:layout_marginTop="100dp"
android:id="@+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</com.example.scroller.view.ScrollCustomView>
</RelativeLayout>
示意图片