【问题标题】:Align three control side by side in Android在Android中并排对齐三个控件
【发布时间】:2015-06-26 09:29:15
【问题描述】:

我是 Android 新手 我想设置这样的控件,但我不能:


文本视图1

编辑文本1 编辑文本2 按钮

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum = "1.0"
    android:orientation="horizontal"
    android:background = "#faadadad" >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="44dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Final"
        android:id="@+id/textView4"
        android:layout_gravity="center_horizontal" />

     <LinearLayout android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="0.8">
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft = "2dip"
        android:layout_marginTop = "1dip"
        android:layout_marginBottom = "1dip"
        android:id="@+id/edit1"/>
</LinearLayout>
<LinearLayout android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="0.8">
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft = "2dip"
        android:layout_marginTop = "1dip"
        android:layout_marginBottom = "1dip"
        android:id="@+id/edit2" />
</LinearLayout>
    <Button android:text = "grade:"
        android:layout_width = "0dip"
        android:layout_height="wrap_content"
        android:layout_weight = "0.2"/>


</LinearLayout>

使用上面的代码,edittext1 和 edittext2 和 textview2 显示在屏幕外和 textview1 下

【问题讨论】:

  • 在第一个LinearLayout中添加android:orientation=vertical
  • 将您的textview 放入您的LinearLayout 部分。
  • @jakombo,我这样做了,但显示如下: [textview1][textview2][button] ,所有这些都在一行中
  • xml 中的第二个 EditText 在哪里?
  • @Tarun ,我这样做了,但什么也没发生

标签: android xml android-layout android-widget alignment


【解决方案1】:

您可以使用此 xml 作为示例,只需替换为您的值

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum = "1.0"
    android:orientation="vertical"
    android:background = "#faadadad" >

    <TextView
        android:id="@+id/textView4"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        android:text="Final"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/EditText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="1dip"
            android:layout_marginLeft="2dip"
            android:layout_marginTop="1dip"
            android:hint="Search Terms" />

        <EditText
            android:id="@+id/EditText1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="1dip"
            android:layout_marginLeft="2dip"
            android:layout_marginTop="1dip"
            android:hint="Another" />

        <Button
            android:id="@+id/Button"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:text="grade:" />
    </LinearLayout>

</LinearLayout>

输出:

【讨论】:

    【解决方案2】:
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    

    删除:

        android:weightSum = "1.0"
    

    改变:

        android:orientation="horizontal"
    

    垂直:

        android:orientation="vertical"
    

    没关系

        android:background = "#faadadad" >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="44dp"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Final"
            android:id="@+id/textView4"
            android:layout_gravity="center_horizontal" />
    

    改变:

        <LinearLayout android:layout_width="0dp"
    

    到:

        <LinearLayout android:layout_width="match_parent"
    

    好的:

            android:layout_height="wrap_content"
    

    删除:

            android:layout_weight="0.8">
    

    好的:

            <EditText
    

    改变:

                android:layout_width="fill_parent"
    

    到:

                android:layout_width="0dp"
    

    好的:

                android:layout_height="wrap_content"
    

    添加:

                android:layout_weight="1"
    

    好的:

                android:layout_marginLeft = "2dip"
                android:layout_marginTop = "1dip"
                android:layout_marginBottom = "1dip"
                android:hint="Search Terms" />
    

    您可以在此处尝试添加另一个 EditText(复制粘贴)

    删除:

        </LinearLayout>
    

    好的:

        <Button android:text = "grade:"
            android:layout_width = "0dip"
            android:layout_height="wrap_content"
    

    改变:

            android:layout_weight = "0.2"/>
    

    到:

            android:layout_weight = "1"/>
    

    添加:

        </LinearLayout>
    

    好的:

    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 2022-06-15
      • 1970-01-01
      • 1970-01-01
      • 2015-08-19
      • 2020-12-12
      • 1970-01-01
      • 2022-01-23
      • 2013-03-13
      • 2016-09-04
      相关资源
      最近更新 更多