【问题标题】:two linear layouts in the same screen同一屏幕中的两个线性布局
【发布时间】:2013-03-25 12:47:53
【问题描述】:

您好,我正在尝试为 Android 手机和平板电脑设计我的应用程序。所以我已经设计了它,但需要重新设计以适应不同屏幕的缩放目的。

这是我想要的布局。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@color/white">
  
    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="2"
        android:orientation="horizontal" >
    
    <fragment
        android:id="@+id/map"
        android:layout_marginTop="5dp"
        android:layout_weight=".1"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        class="com.google.android.gms.maps.MapFragment"
         />
  
    <Button
        android:id="@+id/button3"
        android:layout_weight=".25"
        android:layout_marginLeft="34dp"
        android:layout_marginTop="10dp"
           android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:onClick="ClickTourismButton"
        android:text="@string/Tourism" />
    
    <Button
        android:id="@+id/button4"
        android:layout_weight=".25"
           android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_marginLeft="34dp"
        android:text="@string/Nearby" />
    <Button
        android:id="@+id/button2"
        android:layout_weight=".25"
        android:layout_marginLeft="34dp"
           android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:text="@string/Favourites" />
    <Button
        android:id="@+id/button1"
        android:layout_weight=".25"
          android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_marginLeft="34dp"  
        android:text="@string/Map" />
         
    </LinearLayout>   
    
    
    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weightSum="2"
        android:orientation="horizontal" >
    <TextView
       
        android:layout_marginLeft="10dp"
        android:layout_marginTop="5dp"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:textIsSelectable="true"      
        android:id="@+id/feedupdate1" />
    
    <ListView
        android:id="@android:id/list"
        android:background="@color/red"
        android:layout_weight="1"       
        android:layout_marginTop="5dp"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"/>
    
    <TextView
        android:id="@+id/empty"
        android:textIsSelectable="true"   
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"  
        android:layout_marginLeft="10dp"
         android:layout_marginTop="5dp"/>
    <TextView
        android:id="@+id/feedupdate"        
        android:layout_marginLeft="6dp"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_marginTop="430dp"
        android:textIsSelectable="true"/>
    <ListView
         
        android:id="@+id/list1"
        android:background="@color/blue"
        android:layout_weight="1"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_marginTop="430dp"
        android:layout_marginLeft="6dp" /> 
    
    </LinearLayout>
</RelativeLayout>

当我运行应用程序时,两个线性布局相互重叠??或者上面看到的黑色矩形的地图片段占据了整个屏幕???我不知道我做错了什么,我看了几个教程,似乎是对的?

需要帮助

谢谢


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:weightSum="2"
    android:background="@color/white">

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

    <fragment
        android:id="@+id/map"
        android:layout_marginTop="5dp"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        class="com.google.android.gms.maps.MapFragment"
         />

    <Button
        android:id="@+id/button3"
        android:layout_marginLeft="34dp"
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:onClick="ClickTourismButton"
        android:text="@string/Tourism" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_marginLeft="34dp"
        android:text="@string/Nearby" />
    <Button
        android:id="@+id/button2"
        android:layout_marginLeft="34dp"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:text="@string/Favourites" />
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content" 
        android:layout_height="0dp"
        android:layout_marginLeft="34dp"  
        android:text="@string/Map" />

    </LinearLayout>   


    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >
    <TextView

        android:layout_marginLeft="10dp"
        android:layout_marginTop="5dp"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:textIsSelectable="true"      
        android:id="@+id/feedupdate1" />

    <ListView
        android:id="@android:id/list"
        android:background="@color/red"    
        android:layout_marginTop="5dp"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"/>

    <TextView
        android:id="@+id/empty"
        android:textIsSelectable="true"   
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"  
        android:layout_marginLeft="10dp"
         android:layout_marginTop="5dp"/>
    <TextView
        android:id="@+id/feedupdate"        
        android:layout_marginLeft="6dp"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_marginTop="430dp"
        android:textIsSelectable="true"/>
    <ListView

        android:id="@+id/list1"
        android:background="@color/blue"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_marginTop="430dp"
        android:layout_marginLeft="6dp" /> 

    </LinearLayout>
</LinearLayout>

一个组件占据每一侧?? 它分为两部分,但地图片段占据了一侧,第一个列表视图占据了另一侧???

【问题讨论】:

  • 为两个线性布局制作 android:weight="1"。这将在它们之间平均划分屏幕。
  • 它现在一分为二,但一个组件占用了每一侧的所有空间?如何给每个组件空间?

标签: android android-linearlayout


【解决方案1】:
  1. 将两个子 LinearLayout 的方向更改为 VERTICAL

  2. 对于顶部删除 Relative 并使用具有方向 HORIZONTAL 的 LinearLayout 并拥有weightSum=2

  3. 现在在两个子线性布局视图中都给出layout_width="0dp"layout_weight="1"


为您准备的 XML 示例:http://pastebin.com/2MCBN9qk

【讨论】:

  • 是我应该改变@MKJPrarekh,因为现在它一分为二,但地图片段占据了一侧,第一个列表视图占据了另一侧???
  • 布局看起来不错。然而,列表视图只是显示为细线?
  • 感谢@MKJParekh,您的解决方案对我帮助很大。
【解决方案2】:

尝试使用 LinearLayout 更改 RelativeLayout,请查看此link 以更深入地了解不同类型的布局。

【讨论】:

    【解决方案3】:

    尝试使用 weightSum 并将您的线性布局添加到相对布局中以获得更好的定位。

    【讨论】:

      猜你喜欢
      • 2011-12-14
      • 1970-01-01
      • 2012-09-25
      • 2012-06-13
      • 2017-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多