java 如何将布局放在 android 屏幕键盘的正上方?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6220218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I position a layout right above the android on-screen keyboard?
提问by Sheehan Alam
See the attached photo. Twitter does it well. They have a layout, which I will call a toolbar for lack of a better term, right above the onscreen keyboard. How can I do this with my code?
请参阅随附的照片。Twitter 做得很好。他们有一个布局,我将其称为工具栏,因为没有更好的术语,就在屏幕键盘的正上方。我怎样才能用我的代码做到这一点?
UPDATE: Here is my layout:
更新: 这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff">
<RelativeLayout android:id="@+id/actionbarRelativeLayout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/actionbar_gradient">
<ImageButton android:id="@+id/imageButton" android:layout_width="wrap_content" android:background="@drawable/stocktwits" android:layout_height="wrap_content"></ImageButton>
<TextView android:layout_width="wrap_content" android:id="@+id/charCountTextView" android:text="140" android:layout_alignParentRight="true" android:layout_height="wrap_content" android:textColor="#ffffff" android:textStyle="bold" android:textSize="18sp" android:gravity="center_vertical" android:layout_centerVertical="true"></TextView>
</RelativeLayout>
<EditText android:layout_width="match_parent" android:id="@+id/composeEditText" android:focusable="true" android:hint="Share an idea with the community" android:gravity="left|top" android:layout_height="fill_parent" android:layout_weight="1"></EditText>
<LinearLayout android:layout_width="match_parent" android:id="@+id/border" android:background="#c4c4c4" android:baselineAligned="false" android:layout_height="1dp"></LinearLayout>
<LinearLayout android:layout_height="wrap_content" android:id="@+id/toolbarLinearLayout" android:orientation="horizontal" android:padding="5dip" android:layout_width="fill_parent" android:background="@drawable/gray_toolbar_gradient">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/shortenButton" android:background="@drawable/shortenbutton" android:layout_weight="0"></Button>
<LinearLayout android:layout_height="match_parent" android:layout_width="wrap_content" android:id="@+id/linearLayout1" android:layout_weight="1"></LinearLayout>
<CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/twitterCheckBox" android:textColor="#000000" android:layout_weight="0" android:background="@drawable/twittergraybutton"></CheckBox>
<Button android:layout_height="wrap_content" android:layout_weight="0" android:id="@+id/sendButton" android:layout_width="wrap_content" android:background="@drawable/sharebutton"></Button>
</LinearLayout>
</LinearLayout>
And here is my Manifest where I specify the softInputMode:
这是我的清单,我在其中指定了 softInputMode:
<activity android:name="ShareActivity"
android:theme="@android:style/Theme.NoTitleBar"
android:windowSoftInputMode="adjustResize">
</activity>
回答by adamp
Make sure your soft input mode is set to adjustResize, then place the layout with your toolbar at the bottom of your activity.
确保您的软输入模式设置为 adjustResize,然后将带有工具栏的布局放在您的活动底部。
Example:
例子:
<LinearLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout android:id="@+id/my_content"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1">
<!-- Your content here -->
</FrameLayout>
<LinearLayout android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- Your toolbar items here -->
</LinearLayout>
</LinearLayout>
回答by Ronnie
This is for people who after setting the adjustResize still did not work, there is an article: adjustResize does not work in fullscreen
这是针对设置了adjustResize后还是不行的人,有一篇文章: adjustResize不全屏
I indeed had this set to fullscreen, when setting my theme back to a nonfullscreen theme the layouts resized just as wanted.
我确实将其设置为全屏,当将我的主题设置回非全屏主题时,布局会根据需要调整大小。
回答by sberezin
Important additionto the answers above.
对上述答案的重要补充。
When you work with DialogFragmentfullscreen is applied implicitly.
当您使用DialogFragment 时,全屏被隐式应用。
That means that you have to:
1) set style in onCreate() of your DialogFragment:
这意味着您必须:
1) 在 DialogFragment 的 onCreate() 中设置样式:
public class YourDialogFragment extends DialogFragment {
...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Holo_NoActionBar);
}
}
2) if you use your own theme you should handle it like this:
2)如果你使用自己的主题,你应该这样处理:
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">false</item>
</style>