Cԃա*_*ԃաԃ 6 java xml layout android
我想制作一个具有类似于Facebook应用程序的登录活动/布局的应用程序.我的意思是当文本字段聚焦时,软键盘会推动整个视图,但不会压缩徽标.我尝试过,android:windowSoftInputMode="adjustPan/adjustResize"但这不是我想要实现的目标.
我在SO上发现这个问题也许会让事情变得更清楚,但它没有解决问题的办法.
我也尝试了各种布局类型,但软键盘仅推动聚焦<EditText>.请指导我.
更新:




<?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"
android:orientation="vertical"
android:background="#DDDDDD">
<RelativeLayout
android:height="0dp"
android:layout_weight="1"
android:layout_height="0dp"
android:layout_width="fill_parent"
android:background="#ff0000">
<ImageView
android:layout_centerVertical="true"
android:layout_height="fill_parent"
android:layout_width="fill_parent"></ImageView>
</RelativeLayout>
<RelativeLayout
android:height="0dp"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="fill_parent"
android:background="#00ff00">
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#0000ff"
android:height="0dp" >
<Button
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Log in"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="4dp"
android:hint="password"
android:inputType="textPassword" >
</EditText>
<EditText
android:id="@+id/editText1"
android:hint="login"
android:padding="4dp"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" ></EditText>
</RelativeLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
UPDATE工作解决方案 我不能在这里粘贴整个xml文件,但结构应该足够了.基于Gabe Sechan的回答.
Layout{
Layout top weight 1
Layout mid weight 1
Layout bot weight 1
}
Run Code Online (Sandbox Code Playgroud)
子布局设置为:
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" // should be changed accordingly to your layout design.
Run Code Online (Sandbox Code Playgroud)
这是活动的Java代码(键盘向上/向下):
View top, mid, bot;
final View activityRootView = findViewById(R.id.loginLayout);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int heightDiff = activityRootView.getRootView()
.getHeight() - activityRootView.getHeight();
if (heightDiff > 100) { //keyboard up
mid.setVisibility(View.INVISIBLE);
top.setLayoutParams(new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, 0, 0f));
bot.setLayoutParams(new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, 0, 1f));
} else {// keyboard down
// v.setVisibility(View.VISIBLE);
mid.setVisibility(View.VISIBLE);
top.setLayoutParams(new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, 0, 2f));
bot.setLayoutParams(new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, 0, 3f));
}
}
});
Run Code Online (Sandbox Code Playgroud)
在键盘上你需要根据键盘向上设计更改权重,并在键盘上更改回默认值(通过xml/java设置的布局).我已经在2.3.x及更高版本上测试了代码.并且不要忘记使用android:inputType="textFilter"登录名和密码EditText删除输入建议并保存一些像素.在您的活动清单中android:windowSoftInputMode="adjustResize|stateHidden".stateHidden用于使活动加载时键盘不会启动.希望能帮助到你.祝好运.
他们用相对布局,adjustResize和android:layout_centerVertical来做.基本上,它们的主要布局采用线性布局,内部有3个相同加权的相对布局.每个都设置为0dp高度,因此它们占据屏幕的三分之二.顶部RelativeLayout保持徽标,垂直居中.中间包含登录字段和按钮,垂直居中,一个在另一个上面.底部保存版权文本,与底部对齐.最终结果是,当键盘出现时,3个相对布局的大小调整为新屏幕的1/3.然后他们的元素在新屏幕中居中.
请记住,您需要adjustResize窗口模式才能获得此功能,如果您使用它,它将向上移动,徽标将向中心滚动.
| 归档时间: |
|
| 查看次数: |
7857 次 |
| 最近记录: |