<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
tools:context="${packageName}.${activityClass}" >
<RelativeLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
<ImageView
android:id="@+id/logo"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_centerHorizontal="true"
android:scaleType="centerCrop"
android:src="@drawable/ic_launcher"
tools:ignore="ContentDescription" />
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/logo"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="@string/hello_world"
android:textSize="20sp" />
</RelativeLayout>
<EditText
android:id="@+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/container"
android:layout_margin="16dp"
android:hint="Input sth."
tools:ignore="HardcodedText" />
</RelativeLayout>
public class MainActivity extends Activity {
private View root; // 最外层布局
private View logo; // Logo图标
private View label; // Logo附近的文字
private int rootBottom = Integer.MIN_VALUE;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
root = findViewById(R.id.root);
logo = findViewById(R.id.logo);
label = findViewById(R.id.label);
root.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
root.getGlobalVisibleRect(r);
// 进入Activity时会布局,第一次调用onGlobalLayout,先记录开始软键盘没有弹出时底部的位置
if (rootBottom == Integer.MIN_VALUE) {
rootBottom = r.bottom;
return;
}
// adjustResize,软键盘弹出后高度会变小
if (r.bottom < rootBottom) {
RelativeLayout.LayoutParams lp = (LayoutParams) logo.getLayoutParams();
// 如果Logo不是水平居中,说明是因为接下来的改变Logo大小位置导致的再次布局,忽略掉,否则无限循环
if (lp.getRules()[RelativeLayout.CENTER_HORIZONTAL] != 0) {
// Logo显示到左上角
lp.addRule(RelativeLayout.CENTER_HORIZONTAL, 0); // 取消水平居中
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT); // 左对齐
// 缩小Logo为1/2
int height = logo.getHeight(); // getMeasuredHeight()
int width = logo.getWidth();
lp.width = width / 2;
lp.height = height / 2;
logo.setLayoutParams(lp);
// Logo下的文字
RelativeLayout.LayoutParams labelParams = (LayoutParams) label.getLayoutParams();
labelParams.addRule(RelativeLayout.CENTER_HORIZONTAL, 0); // 取消水平居中
labelParams.addRule(RelativeLayout.BELOW, 0); // 取消显示到logo的下方
labelParams.addRule(RelativeLayout.RIGHT_OF, R.id.logo); // 显示到Logo的右方
labelParams.addRule(RelativeLayout.CENTER_VERTICAL); // 垂直居中
label.setLayoutParams(labelParams);
}
} else { // 软键盘收起或初始化时
RelativeLayout.LayoutParams lp = (LayoutParams) logo.getLayoutParams();
// 如果没有水平居中,说明是软键盘收起,否则是开始时的初始化或者因为此处if条件里的语句修改控件导致的再次布局,忽略掉,否则无限循环
if (lp.getRules()[RelativeLayout.CENTER_HORIZONTAL] == 0) {
// 居中Logo
lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
// 还原Logo为原来大小
int height = logo.getHeight();
int width = logo.getWidth();
lp.width = width * 2;
lp.height = height * 2;
logo.setLayoutParams(lp);
// Logo下的文字
RelativeLayout.LayoutParams labelParams = (LayoutParams) label.getLayoutParams();
labelParams.addRule(RelativeLayout.CENTER_HORIZONTAL); // 设置水平居中
labelParams.addRule(RelativeLayout.BELOW, R.id.logo); // 设置显示到Logo下面
labelParams.addRule(RelativeLayout.RIGHT_OF, 0); // 取消显示到Logo右面
labelParams.addRule(RelativeLayout.CENTER_VERTICAL, 0); // 取消垂直居中
label.setLayoutParams(labelParams);
}
}
}
});
}
}
public static final int TRUE = -1;
public void addRule(int verb) {
mRules[verb] = TRUE;
mInitialRules[verb] = TRUE;
mRulesChanged = true;
}
public void addRule(int verb, int anchor) {
mRules[verb] = anchor;
mInitialRules[verb] = anchor;
mRulesChanged = true;
}
public void removeRule(int verb) {
mRules[verb] = 0;
mInitialRules[verb] = 0;
mRulesChanged = true;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有