【问题标题】:In Android, I am having 2 Buttons and one Text. I have to arrange one button left, Text in Middle, and One Button in Right. How can I do this?在 Android 中,我有 2 个按钮和一个文本。我必须在左边安排一个按钮,在中间安排一个按钮,在右边安排一个按钮。我怎样才能做到这一点?
【发布时间】:2019-03-28 10:26:12
【问题描述】:

我正在使用水平线性布局。

在同一行中,我想在左侧放置一个按钮,将文本放在中间,并将另一个按钮放在右侧。

如何实现?

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 由于这是 UI 任务,请通过图片向我们展示您想要完成的任务以及目前取得的成果。

标签: android layout alignment placement


【解决方案1】:

同样的 XML 方法:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum=".9"
    >

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".3"
        android:text="B1"
        />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".3"
        />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".3"
        android:text="B2"
        />
</LinearLayout>

如果您想调整单个项目的宽度,可以更改 'android:layout_weight' 属性

【讨论】:

    【解决方案2】:

    要在 LinearLayout 中均匀分布元素,您需要使用 weight。你可以这样做:

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/selector_default"
        android:orientation="horizontal"
        >
    
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Text"
            />
    
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            />
    
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Text"
            />
    </LinearLayout>
    

    【讨论】:

    • 谢谢。上面的代码是把一个按钮放在左边,另一个放在 .正确并且中间对齐文本?
    • 是的。如果要对齐按钮内的文本,可以为按钮设置android:gravity="center"
    • 感谢所有人。经过长期的努力,我终于找到了解决方案。我在这里给出我的解决方案。以便对其他人有所帮助。我以编程方式完成了这项工作。
    • 第一个按钮代码:homeButton = new ImageView(this); homeButton.setImageResource(R.drawable.ic_menu_white_36dp); homeButton.setId(generateViewId()); RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE); params1.addRule(RelativeLayout.CENTER_IN_PARENT,RelativeLayout.TRUE); homeButton.setLayoutParams(params1); relativeLayout.addView(homeButton);
    • 第二个TextView代码:TextView projectTitle = new TextView(this);项目标题.setText(...);项目标题.setTextSize(20); RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); params2.addRule(RelativeLayout.CENTER_IN_PARENT,RelativeLayout.TRUE); projectTitle.setLayoutParams(params2); projectTitle.setTextColor(Color.WHITE); relativeLayout.addView(projectTitle);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多