简体   繁体   English

Android GUI:布局管理器

[英]Android GUI: Layout manager

I'm learning about Layout managers to improve my app design. 我正在学习布局管理器,以改善我的应用程序设计。 For the most part it worked out ok. 在大多数情况下,都可以。 The only thing I want is my buttons to be positioned a little higher.I can achieve this by removing the 3 buttons from the layout manager but by doing this I have issues on lower res devices. 我唯一想做的是将按钮定位得更高一点,我可以通过从布局管理器中删除3个按钮来实现这一点,但是这样做会导致分辨率较低的设备出现问题。 (The buttons will position themselves over the EditText fields or position themselves over my ad at the bottom). (按钮将其自身定位在EditText字段上,或将其自身定位在我的广告底部)。 I've tried some things I found online but I only ended up ruining the layout. 我尝试了一些在网上找到的东西,但最终却破坏了版面。

Here is my XML file: 这是我的XML文件:

     <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:id="@+id/linearLayout3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <LinearLayout
            android:id="@+id/textviewLayoutLeft"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tvPpl"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingBottom="10dp"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:paddingTop="20dp"
                android:text="Price Per Liter"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/tvAvgConsumption"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingBottom="10dp"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:paddingTop="10dp"
                android:text="avg(l/100km)"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/tvDistance"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:text="Distance (km)"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/tvAmountOfPersons"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingBottom="10dp"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:paddingTop="10dp"
                android:text="# Persons"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/textView5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:paddingTop="20dp"
                android:text="Price Per Person"
                android:textStyle="bold" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/textfieldLayoutRight"
            android:layout_width="166dp"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <EditText
                android:id="@+id/textPrijsPerLiter"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="numberDecimal" />

            <EditText
                android:id="@+id/textVerbruik"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:ems="10"
                android:inputType="numberDecimal" />

            <EditText
                android:id="@+id/textAfstand"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="numberDecimal" />

            <EditText
                android:id="@+id/textAantalPersonen"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:ems="10"
                android:imeOptions="actionGo"
                android:inputType="numberDecimal" />

            <TextView
                android:id="@+id/textViewPPP"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="10dp"
                android:paddingTop="20dp"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="#ffff000f"
                android:textStyle="bold" />

        </LinearLayout>

    </LinearLayout>


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="100dp"
            android:layout_marginTop="10dp"
            android:orientation="vertical">


            <Button
                android:id="@+id/btnBereken"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Calculate"
                android:textStyle="bold" />

            <Button
                android:id="@+id/btnReset"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Reset all textfields"
                android:textStyle="bold" />


            <Button
                android:id="@+id/btnLoad"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Load latest results"
                android:textStyle="bold" />

        </LinearLayout>

        <com.google.android.gms.ads.AdView
            android:id="@+id/adView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ads:adSize="SMART_BANNER"
            ads:adUnitId="MY_ID" />
    </LinearLayout>

</RelativeLayout>

You can: 您可以:

  1. Give up second linear layout and move button layout and ad layouts to main layout. 放弃第二个线性布局,并将按钮布局和广告布局移至主布局。 Stick ad layout to bottom and declare it BEFORE button layout. 将广告布局放在底部,并在按钮布局之前声明它。 Set buttons adjustments to layout_below="textfieldlayout" and layout_above="ad" instead of layout_alignParentBottom="true". 将按钮调整设置为layout_below =“ textfieldlayout”和layout_above =“ ad”,而不是layout_alignParentBottom =“ true”。

  2. Consider using linear layout with weightSum. 考虑将线性布局与weightSum一起使用。 Every parent layout should have weightSum=100, every child will respectively have % of space. 每个父级布局应具有weightSum = 100,每个子级分别具有%的空间。 From my point of view this way would be the best, because views will position equally for merely all devices and even layout tree depth will remain the same. 从我的角度来看,这是最好的方法,因为对于所有设备而言,视图的位置将相等,甚至布局树的深度也将保持不变。 You'll only have to change the parent layout to LinearLayout and set weights. 您只需要将父布局更改为LinearLayout并设置权重即可。

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

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