2016-06-15 7 views
0

Ich habe Probleme mit dem Layout eines linearen Layout mit Elementen, die nebeneinander sind. Ich kann sie innerhalb eines linearen Layouts Seite an Seite gehen lassen, indem ich die Elemente in eine TableView setze, aber ich kann nicht die richtige Ausrichtung auf sie anwenden. z.B. die TextView1 ausrichten müssen links und sein Partner TextView2 ist richtig Auch die Taste ausrichten wird nicht ausrichten Rechts, auch wenn ichWie Layout Elemente nebeneinander auf einem linearen Layout und Anwendung richtigen Abstand und Ausrichtung

android:layout_alignParentRight="true" 

verwenden Ich versuche, eine Quittung Tätigkeit, wo der Titel „SubTotal“, „Steuer“ zu schaffen, „Total“ ausgerichtet sind, nach links und ihre Werte ausgerichtet ist auf der rechten Seite Unten ist mein xml

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" 
    android:layout_height="match_parent" 

    android:paddingTop="20dp" 
    android:paddingLeft="20dp" 
    android:paddingRight="20dp" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:showIn="@layout/app_bar_main" 
    tools:context=".MainActivity" 
    android:elevation=".5dp" 
    android:focusable="true" 
    android:layout_centerHorizontal="true"> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_centerHorizontal="true" 
    android:orientation="vertical" 
    android:gravity="bottom"> 

    <TextView 

     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="SAN AGUSTIN MUSEUM" 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/textViewTicketName"/> 
    <TextView 

     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingBottom="20dp" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="(Comes with Audio Guide)" 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/textViewTicketDesc"/> 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/border" 
     android:orientation="vertical" 
     android:gravity="bottom"> 



     <TableRow 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

     <TextView 
      android:id="@+id/TblText1" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="left" 
      android:text="Subtotal:"/> 

     <TextView 
      android:id="@+id/TblText11" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="right" 
      android:text="$5.00" 
      android:layout_alignParentRight="true"/> 
     </TableRow> 

     <TextView 

     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dp" 

     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Subtotal $5.00" 
     android:paddingLeft="0dp" 
     android:layout_below="@+id/textViewTicketName" 

     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/textSubtotal"/> 




     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:text="Tax $0.50" 
      android:paddingLeft="0dp" 
      android:layout_below="@+id/textViewTicketName" 

      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/textTax"/> 

     <View android:background="#000" android:layout_width="match_parent" android:layout_height="1px"/> 

     <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="10dp" 

     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Total $5.50" 
     android:paddingLeft="0dp" 
     android:layout_below="@+id/textTotal" 

     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/textViewTotalAmount"/> 

    </LinearLayout> 

    <Button 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text=" PURCHASE " 
     android:id="@+id/hiaButton" 
     android:onClick="hiaGetTicket" 
     android:paddingTop="8dp" 
     android:paddingBottom="8dp" 
     android:layout_marginTop="10dp" 
     android:layout_alignParentRight="true" 

     android:textSize="18sp" 
     android:textColor="@color/colorWhite" 
     android:alpha="20" 

     android:background="@drawable/buttonround"/> 

</LinearLayout> 
</ScrollView> 

Dies ist, was ich bin auf dem Ziel. In der XML gibt es 2 Zwischensummenfelder, weil ich die TablView ausprobiert habe. Es muss nebeneinander sein, aber die Beschriftung auf der rechten Seite ist gerechtfertigt, rechts, während die Werte auf der rechten Seite rechts auch

enter image description here

gerechtfertigt ist

Bisher in diesem was geschieht die XML unter mit

enter image description here

+0

Haben Sie eine Rasteransicht versucht? –

+1

Das Hinzufügen eines Bildes/einer Vorlage des erwarteten Ergebnisses kann hilfreich sein. Außerdem unterstützt linearLayout nicht layout_alignParentRight. – Ozgur

+0

@Malith Nö können Sie ein Beispiel als Antwort geben? Ich habe versucht, verschiedene Layouts in Layouts mit horizontaler Ausrichtung und andere aber nicht Raster – TwoThumbSticks

Antwort

2

Verwenden layout_weight einen Raum zwischen zwei Ansichten zu erstellen.

<!-- Subtotal row --> 
<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="80dp" 
orientation:"horizontal"> 

    <!--txt--> 
    <TextView 
    android:layout_width="120dp" 
    android:layout_height="wrap_content" 
    android:text="Subtotal" 
    android:gravity="right" 
    /> 

    <!-- empty space that will fill "subtotal" and "price", using layout_weight --> 
    <View 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:layout_height="1dp"/> 

    <!-- price --> 
    <TextView 
    android:layout_width="20dp" 
    android:layout_height="wrap_content" 
    android:text="Subtotal" 
    /> 

</LinearLayout> 

layout_weight ermöglicht es uns, Kind Ansichten zu organisieren auf der Grundlage ihrer "Gewicht" s. Betrachten Sie jene Beispiele:

1. Beispiel

<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
orientation:"horizontal" 
android:weightSum="300" --> total sum of child's weight 
> 

<Button 
     android:layout_width="0dp" 
     android:layout_weight="100" -> %33 
     android:layout_height="1dp" 
     android:text="Button1" 
/> 

<Button 
     android:layout_width="0dp" 
     android:layout_weight="100" -> %33 
     android:layout_height="1dp" 
     android:text="Button2" 
/> 

<Button 
     android:layout_width="0dp" 
     android:layout_weight="100" -> %33 
     android:layout_height="1dp" 
     android:text="Button3" 
/> 

</LinearLayout> 

Vorschau:

____Button1________Button2________Button3____ (gleiche Breiten, und es werden 1% space)

210 2. Beispiel

<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
orientation:"horizontal" 
android:weightSum="80" --> total sum of child's weight 
> 

<Button 
     android:layout_width="0dp" 
     android:layout_weight="40" -> %50 
     android:layout_height="1dp" 
     android:text="Button1" 
/> 

<Button 
     android:layout_width="0dp" 
     android:layout_weight="20" -> %25 
     android:layout_height="1dp" 
     android:text="Button2" 
/> 

<Button 
     android:layout_width="0dp" 
     android:layout_weight="20" -> %25 
     android:layout_height="1dp" 
     android:text="Button3" 
/> 

</LinearLayout> 

Vorschau:

________Button1____________Button2________Button3____ (erste ist breiter)

3.Beispiel Ihr Fall

<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="80dp" 
orientation:"horizontal"> 

    <!--txt--> 
    <TextView 
    android:layout_width="120dp" 
    android:layout_height="wrap_content" 
    android:text="Subtotal" 
    android:gravity="right" 
    /> 

    <View 
    android:layout_width="0dp" 
    android:layout_weight="777" --> this will fill empty space, we didn't specify weightSum, so it thinks weightSum is 777. 
    android:layout_height="5dp" 
    android:background="#ff0000" 
    /> 

</LinearLayout> 
+0

Es funktionierte tatsächlich, aber nicht nach dem Experimentieren mit verschiedenen DP-Werten für das erste und zweite Textelement, bis sie in eine einzelne Zeile passten. Ich verstehe nicht vollständig, wie die layout_weight = "1" funktioniert und warum die 1 ist der richtige Wert für das Gewicht – TwoThumbSticks

+0

@TwoThumbSticks Ich habe meine Antwort bearbeitet. – Ozgur

+0

Danke jetzt ist es auch eine nette Referenz für andere. Ich habe deine Antwort gerade jetzt angenommen :) – TwoThumbSticks

2

Mit Relative-Layout Sie es auf diese Weise tun können,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/main_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <TextView 
     android:id="@+id/firsttext" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:text="@string/hello_world" /> 

    <TextView 
     android:id="@+id/secondtext" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:text="@string/hello_world" /> 

</RelativeLayout> 

Und wenn Sie mit Linearlayout tun möchten, müssen Sie weightsum und Gewicht für Textview verwenden.

Verwandte Themen