【问题标题】:what is the best android layout什么是最好的android布局
【发布时间】:2013-09-19 23:19:31
【问题描述】:

我同时使用了 RelativeLayout 和 Linearlayout 来实现这一点。(在图片中)!

  1. 使用相对布局,我以固定宽度和 marginLeft 和 marginRight 定位按钮。问题是,如果设备有更大的屏幕,左右边距不会扩大太多,按钮宽度也是固定的。

     <RelativeLayout>
    
        <!-- Row1 -->
    
    <Button1
      alignParentTop = "true" 
      marginLeft = "5dip"
      marginRight="5dip"
     width="80dip"/>
    
    <button2
     alignParentTop ="true"
     center="@id/Button1"
     marginLeft = "5dip"
     marginRight="5dip"
     width="80dip" />
    
    <button2
     alignParentRight="true"
     marginLeft = "5dip"
     marginRight="5dip"
     width="80dip"/>
    
    
    <!-- Row2 -->
    
    <Button3
      below="button1"
      marginLeft = "5dip"
      marginRight="5dip"
     width="80dip"/>
    
    <Button4
      below="button2"
      marginLeft = "5dip"
      marginRight="5dip"
     width="80dip"/>
    
        </RelativeLayout>
    
  2. 使用 RelativeLayout 父级,创建 2 个带有按钮的水平线性布局。现在按钮的宽度为 0dip,权重为 1。但是,我无法创建第二行。



 <RelativeLayout>

   <linearLayout orientation="horizontal">
     <!-- Row1 -->
     <Button1     
     width="0dip"
     weight="1"/>

    <button2
     width="0dip"
     weight="1"/>

    <button2
     width="0dip"
     weight="1"/>
     </linearLayout>
<!--Row2-->
      <linearLayout orientation="horizontal">
     Couldn't using this approach
       </linearLayout>
</RelativeLayout>

【问题讨论】:

    标签: android android-layout android-xml


    【解决方案1】:

    您当然可以像其他人建议的那样使用TableLayoutGridLayout,但如果您想修改您的LinearLayout 解决方案,您只需稍作改动即可实现。像这样的:

    <LinearLayout android:orientation="vertical">   
       <!-- Row1 -->
       <LinearLayout android:orientation="horizontal">
         <Button    
         android:layout_width="0dip"
         android:layout_weight="1"/>
    
        <Button
         android:layout_width="0dip"
         android:layout_weight="1"/>
    
        <Button
         android:layout_width="0dip"
         android:layout_weight="1"/>
       </LinearLayout>
    
      <!-- Row2 -->
      <LinearLayout android:orientation="horizontal" android:weightSum="3">    
         <Button    
         android:layout_width="0dip"
         android:layout_weight="1"/>
    
        <Button
         android:layout_width="0dip"
         android:layout_weight="1"/>
      </LinearLayout>
    </LinearLayout>
    

    注意第二行的weightSum加起来是3,但是每个按钮的权重只有1。

    【讨论】:

    • 完美!我不喜欢 GridLayout,因为它有一些空间问题。我当然可以尝试 TableLayout,但 LinearLayout 更简单。
    【解决方案2】:

    我会选择Gridlayout 来实现这个功能。更少的代码主要和更好地处理方向变化。

    【讨论】:

      【解决方案3】:

      如果我理解正确,您希望将按钮拉长以填满屏幕。尝试改用TableLayout。一个例子是here。基本上,您为按钮使用表格行和列,并指定“stretchColumns”以确保您的列(以及按钮)伸展以填充空间。

      【讨论】:

      • 不,第二行只有2个按钮。
      【解决方案4】:

      正如@prijupaul 所说,您可以使用 Gridlayout,但为了简单起见,我会坚持使用 LinearLayout。我不知道您为什么将所有内容都包装在 RelativeLayout 中,我认为您可以避免这种情况。

      <linearLayout  orientation="vertical">   
         <LinearLayout orientation="horizontal">
           <!-- Row1 -->
           <Button1     
           width="0dip"
           weight="1"/>
      
          <button2
           width="0dip"
           weight="1"/>
      
          <button2
           width="0dip"
           weight="1"/>
         </LinearLayout>
      
        <!-- Row2 -->
        <LinearLayout orientation="horizontal">    
           <Button1     
           width="0dip"
           weight="1"/>
      
          <button2
           width="0dip"
           weight="2"/>
      </LinearLayout>
      

      【讨论】:

      • 如果我使用weight=2,它会占用两倍的空间(2个按钮空间)
      • 我会做类似的事情。但是要解决您评论中的问题,您可以将第二个Buttonweight 保留为1,并创建一个空的&lt;View>,并为其赋予与Buttons 相同的layout 属性。如果你想要一个空的空间有相同的空间
      猜你喜欢
      • 2017-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多