activity button切换时怎么让下面一排button保持不动

参考项目[KeyboardVisibilityEvent](/yshrsmz/KeyboardVisibilityEvent)
1、AndroidManifest.xml中activity不设置android:windowSoftInputMode属性或者设置为adjustUnspecified
2、修改过后的KeyboardVisibilityEvent代码
public class KeyboardVisibilityEvent {
private final static int KEYBOARD_VISIBLE_THRESHOLD_DP = 100;
* Set keyboard visibility change event listener.
* @param activity Activity
* @param listener KeyboardVisibilityEventListener
public static void setEventListener(final Activity activity, final boolean isFullScreen,
final KeyboardVisibilityEventListener listener) {
if (activity == null) {
throw new NullPointerException(&Parameter:activity must not be null&);
if (listener == null) {
throw new NullPointerException(&Parameter:listener must not be null&);
final View activityRoot = getActivityRoot(activity);
activityRoot.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
private final Rect rootViewRect = new Rect();
private final int visibleThreshold = getVisibleThreshold(activity);
private boolean wasOpened =
public void onGlobalLayout() {
activityRoot.getWindowVisibleDisplayFrame(rootViewRect);
int heightDiff = activityRoot.getRootView().getHeight() - rootViewRect.height();
if(!isFullScreen)
heightDiff -= getStatusBarHeight(activity);
boolean isOpen = heightDiff & visibleT
if (isOpen == wasOpened) {
// keyboard state has not changed
wasOpened = isO
listener.onVisibilityChanged(isOpen, heightDiff);
* Determine if keyboard is visible
* @param activity Activity
* @return Whether keyboard is visible or not
public static boolean isKeyboardVisible(Activity activity) {
Rect r = new Rect();
View activityRoot = getActivityRoot(activity);
int visibleThreshold = getVisibleThreshold(activity);
activityRoot.getWindowVisibleDisplayFrame(r);
int heightDiff = activityRoot.getRootView().getHeight() - r.height();
return heightDiff & visibleT
public static int getVisibleThreshold(Context context)
Math.round(UIUtil.convertDpToPx(context, KEYBOARD_VISIBLE_THRESHOLD_DP));
private static View getActivityRoot(Activity activity) {
return ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);
private static int getStatusBarHeight(Context context) {
int result = 0;
int resourceId = context.getResources().getIdentifier(&status_bar_height&, &dimen&, &android&);
if (resourceId & 0) {
result = context.getResources().getDimensionPixelSize(resourceId);
3、布局文件
&RelativeLayout xmlns:android=&/apk/res/android&
xmlns:tools=&/tools&
android:layout_width=&match_parent&
android:layout_height=&match_parent&
tools:context=&.MainActivity&&
&RelativeLayout
android:id=&@+id/rl_bottom&
android:layout_width=&match_parent&
android:layout_height=&match_parent&&
android:layout_width=&match_parent&
android:layout_height=&200dp&
android:gravity=¢er_horizontal|top&
android:layout_alignParentTop=&true&
android:text=&顶部&
android:textColor=&#ff0000&/&
android:layout_width=&match_parent&
android:layout_height=&200dp&
android:gravity=¢er&
android:layout_centerInParent=&true&
android:text=&中间&
android:textColor=&#ff0000&/&
android:layout_width=&match_parent&
android:layout_height=&200dp&
android:gravity=¢er_horizontal|bottom&
android:layout_alignParentBottom=&true&
android:text=&底部&
android:textColor=&#ff0000&/&
&/RelativeLayout&
&RelativeLayout android:id=&@+id/rl_top&
android:layout_width=&match_parent&
android:layout_height=&match_parent&&
android:id=&@+id/keyboard_status&
android:text=&@string/hello_world&
android:layout_alignParentTop=&true&
android:layout_width=&match_parent&
android:gravity=¢er&
android:textSize=&30sp&
android:layout_height=&100dp& /&
&LinearLayout
android:layout_width=&match_parent&
android:layout_height=&wrap_content&
android:layout_alignParentBottom=&true&
android:orientation=&vertical&&
&LinearLayout
android:layout_width=&match_parent&
android:layout_height=&wrap_content&
android:background=򡈠ff&
android:gravity=¢er_vertical&
android:orientation=&horizontal&&
android:id=&@+id/button&
android:text=&点我点我&
android:gravity=¢er&
android:layout_alignParentTop=&true&
android:layout_width=&wrap_content&
android:layout_height=&40dp& /&
android:id=&@+id/text_field&
android:layout_alignParentBottom=&true&
android:layout_width=&match_parent&
android:layout_height=&40dp& /&
&/LinearLayout&
android:gravity=¢er&
android:background=ᧈff00&
android:layout_width=&match_parent&
android:layout_height=&60dp& /&
&/LinearLayout&
&/RelativeLayout&
&/RelativeLayout&
4、MainActivity代码
public class MainActivity extends AppCompatActivity {
TextView mKeyboardS
EditText mTextF
RelativeLayout rlB
RelativeLayout rlT
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mKeyboardStatus = (TextView) findViewById(R.id.keyboard_status);
mTextField = (EditText) findViewById(R.id.text_field);
button = (TextView) findViewById(R.id.button);
rlTop = (RelativeLayout) findViewById(R.id.rl_top);
rlBottom = (RelativeLayout) findViewById(R.id.rl_bottom);
KeyboardVisibilityEvent.setEventListener(this, false, new KeyboardVisibilityEventListener() {
public void onVisibilityChanged(boolean isOpen, int kh) {
updateKeyboardStatusText(isOpen);
Log.i(&aaaaaaaaaaaaab&, &isOpen: & + isOpen + &
kh: & + kh);
if(isOpen)
RelativeLayout.LayoutParams p = (RelativeLayout.LayoutParams) rlTop.getLayoutParams();
p.height = rlBottom.getHeight() -
rlTop.setLayoutParams(p);
if(kh & KeyboardVisibilityEvent.getVisibleThreshold(MainActivity.this))
RelativeLayout.LayoutParams p = (RelativeLayout.LayoutParams) rlTop.getLayoutParams();
p.height = rlBottom.getHeight();
rlTop.setLayoutParams(p);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
RelativeLayout.LayoutParams p = (RelativeLayout.LayoutParams) rlTop.getLayoutParams();
p.height = rlBottom.getHeight() - (mKh & 200 ? mKh : 600);
rlTop.setLayoutParams(p);
UIUtil.showKeyboard(MainActivity.this, mTextField);
updateKeyboardStatusText(KeyboardVisibilityEvent.isKeyboardVisible(this));
private void updateKeyboardStatusText(boolean isOpen) {
mKeyboardStatus.setText(String.format(&keyboard is %s&, (isOpen ? &visible& : &hidden&)));2013年10月 Java大版内专家分月排行榜第二2013年3月 Java大版内专家分月排行榜第二2013年2月 Java大版内专家分月排行榜第二
2013年7月 Java大版内专家分月排行榜第三2013年5月 Java大版内专家分月排行榜第三2013年4月 Java大版内专家分月排行榜第三
本帖子已过去太久远了,不再提供回复功能。

参考资料

 

随机推荐