如何在Android屏幕键盘上方放置布局?

时间:2011-06-02 20:49:08

标签: java android keyboard android-linearlayout

参见附图。 Twitter做得很好。他们有一个布局,我称之为工具栏,缺少一个更好的术语,就在屏幕键盘上方。我怎么能用我的代码做到这一点?

enter image description here

更新: 这是我的布局:

<?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>

这是我的Manifest,我在其中指定了softInputMode:

<activity android:name="ShareActivity"
              android:theme="@android:style/Theme.NoTitleBar"
              android:windowSoftInputMode="adjustResize">
    </activity>

3 个答案:

答案 0 :(得分:32)

确保您的soft input mode is set to adjustResize,然后将工具栏的布局放在活动的底部。

示例:

<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>

答案 1 :(得分:10)

这对于设置了adjustResize之后仍然无效的人,有一篇文章: adjustResize does not work in fullscreen

我确实把这个设置为全屏,当我将主题设置回非完美主题时,布局按照需要调整大小。

答案 2 :(得分:0)

上述答案的重要添加

当您使用 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)如果你使用自己的主题,你应该像这样处理它:

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">false</item>
</style>
相关问题