【问题标题】:Two widgets one next to other in linear layout线性布局中两个相邻的小部件
【发布时间】:2012-09-10 18:13:48
【问题描述】:

我觉得很难实现,线性布局中的两个小部件(比如微调器)一个挨一个。我的意思是两个微调器的布局高度是包装内容,但宽度应该是第一个微调器的前半部分,屏幕的后半部分到第二个微调器。在线性布局中,它们一个接一个。我尝试了相对布局,但是当我将宽度指定为 wrap_content 时,两者都彼此相邻,但第二个微调器仍留有大量空间。我已经在几个应用程序中看到了这一点,但我没有得到它。

【问题讨论】:

    标签: android android-layout android-spinner


    【解决方案1】:

    使用layout_weight。这将迫使两个微调器各自占据一半的空间。

    <LinearLayout
        android:orientation="horizontal"
        ... >
    
        <Spinner
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:layout_weight="1"
            ... />
    
        <Spinner
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:layout_weight="1"
            ... />
    
    </LinearLayout>
    

    【讨论】:

    • 没有。这是一个horizontal 布局,所以权重将被水平应用。
    • 在我的布局中,我有一个如上图所示的微调器,因此我在其中使用了另一个线性布局并将其方向设置为水平。我会发布那个答案,但那是受你的回答启发的,所以我接受你的:)
    【解决方案2】:

    有一天我遇到了完全相同的问题。我还尝试了很多不同的技巧来让它发挥作用。我最终将两个微调器放在自己的布局中。很奇怪,但它有效。

    【讨论】:

    • 我想我可以通过提及明确的大小来做到这一点..但这不是实现它的正确方法
    【解决方案3】:

    相对布局用于放置彼此相关的视图。它与它们的大小无关。

    对于 linearLayout ,将两个视图的高度/宽度(取决于布局的方向)设置为 0px ,将权重设置为 1 。这将使每个占用一半的空间。

    【讨论】:

      【解决方案4】:
      <?xml version="1.0" encoding="utf-8"?>
      

      <Spinner
          android:id="@+id/spinner1"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:entries="@array/testArray" />
      
      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:background="@android:color/background_light"
          android:orientation="horizontal" >
      
          <Spinner
              android:id="@+id/spinner2"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:entries="@array/testArray" />
      
          <Spinner
              android:id="@+id/spinner3"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight="1" />
      </LinearLayout>     
      </LinearLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-15
        • 2012-09-20
        • 2020-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多