繁体   English   中英

我应该使用哪种布局?

[英]Which of layout should i use?

我糊涂了。 我想显示地图,并在地图下方显示5个按钮。 我使用RelativeLayout,但是程序只显示Product按钮。 为什么? 我很困惑我使用哪种布局(线性,相对,框架或绝对)! 请帮我。 以及我该如何更正此代码?

location.xml:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frame"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.MapView
    android:id="@+id/mapView"
    android:apiKey="0cPRv243zM1_S3ydsNg8MJP9_6BfCp642jOhPvQ"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true" />
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/background"
    android:layout_gravity="bottom"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button_home"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableTop="@drawable/home_icon"
        android:text="@string/button_home"
        android:textColor="@color/text_home" />

     <Button
        android:id="@+id/button_product"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableTop="@drawable/product_icon"
        android:onClick="Product"
        android:text="@string/button_product"
        android:textColor="@color/text_product" />

  </LinearLayout>
  </LinearLayout>

要解决您的特定问题:您应该说产品按钮在主页按钮的右边,而不是说主页按钮在产品按钮的左边。 放大RelativeLayout时,将以线性方式解析布局,因此,如果视图A相对于视图B定位,则视图B必须首先出现。

<Button
    android:id="@+id/button_home"
    style="?android:attr/borderlessButtonStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawableTop="@drawable/home_icon"
    android:text="@string/button_home"
    android:textColor="@color/text_home"/>

<Button
    android:id="@+id/button_product"
    style="?android:attr/borderlessButtonStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/button_home"
    android:drawableTop="@drawable/product_icon"
    android:onClick="Product"
    android:text="@string/button_product"
    android:textColor="@color/text_product" />

将此添加到产品按钮,然后从主页按钮删除layout_toLeftOf。

  android:layout_toRightOf="@id/button_home" 

您可以使用重力和对齐方式来放置主屏幕按钮,然后再放置其他四个按钮,每个按钮都位于其前面的右侧。

祝好运

  • LinearLayout :当我们需要水平或垂直排列小部件/视图时,可使用LinearLayout。 排列方向可以设置为水平或垂直,默认情况下为水平。

  • TableLayout :如果需要以行和列的形式排列Layout的窗口小部件/视图,则使用此布局对象。 这类似于html表。 单元格可以跨越列。 TableLayout不显示其边框。 可以通过设置列的相应属性来缩小和拉伸,“ TableRow”是另一个助手小部件,应与TableLayout结合使用。

  • RelativeLayout :这里每个小部件/视图的位置都是相对的/相互依赖的。 例如,当需要一种布局时,它的文本视图位于编辑文本框的左侧,而按钮位于EditText下方。 视图之间的关系要经过一次迭代,因此,如果视图B的位置取决于视图A的位置,则视图A必须在布局中排在第一位。

  • FrameLayout :这是一个非常简单的布局,用于将屏幕的一部分保留为空白,以便在运行时显示一个或一组项目。 框架布局中添加的所有元素都将添加到屏幕的左上方。

  • AbsoluteLayout :当需要指定视图的确切x和y坐标位置时,则需要使用AbsoluteLayout。 这种布局很难维护。

默认情况下,RelativeLayout将这两个按钮放在一起,因此您只能看到后者。

和线

android:layout_toLeftOf="@+id/button_product"

是错的。 @+id创建一个id,在这种情况下使用@id

我会针对这种情况建议使用LinearLayout。 在其中放置这些按钮,并与一些保证金进行调整。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM