【问题标题】:How to align a button to bottom of screen如何将按钮与屏幕底部对齐
【发布时间】:2013-12-09 13:02:41
【问题描述】:

我在我的 WebView 中添加了一个按钮。但我无法让它与屏幕底部对齐。我已经给出了下面的代码。

Button button = new Button(context);
button.setText("Button");
LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
button.setLayoutParams(params1);
webView.addView(button);

有谁知道怎么做。

【问题讨论】:

  • 你试过重力底吗?
  • @Pull 我在任何地方都没有找到该选项
  • 我能知道你不是想把那个按钮放在网页视图下面吗? y 在 webview 中?
  • params1.gravity = Gravity.BOTTOM;
  • @Pull 没有名为 params1.gravity 的选项

标签: android button android-relativelayout android-framelayout


【解决方案1】:

为了您的关心,试试这样:;

Button btn1 = (Button) findViewById(R.id.btn1);

        btn1.setGravity(Gravity.BOTTOM);

更新 :: 为你需要放置在屏幕底部的按钮尝试这样的事情

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:gravity="center|bottom"
        android:orientation="vertical" >


        <Button
            android:id="@+id/button3"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|center"
            android:text="3" />
    </LinearLayout>

【讨论】:

  • @AndroidHacker 我不想从布局中加载它。
  • @glo 想要在 webview 中添加该按钮,而不是线性或相对布局
  • @AndroidHacker 我不想从布局 xml 添加按钮,我找到了解决方案
【解决方案2】:

您可以尝试使用 LinearLayout 和重力:

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params.gravity=Gravity.BOTTOM;

【讨论】:

  • 这对我不起作用。它给出了相同的结果。按钮在顶部。
【解决方案3】:

好的,我得到了解决方案。我做了button.setY(screenHeight-button.getHeight());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多