简体   繁体   中英

Android - how to set margin relative to another control?

I'm really new to android and would appreciate any help from you. I have a screen with different templates for two kind of products. Each template is a separate xml file where I place a bunch of controls. Every control has it's own exact place in the screen which I define with margins. The problem is I don't know how long is going to be a text in the upper TextView. If there is a one line everything looks fine, but a few lines destroy it. Is there any way to stretch upper textview and at the same time keep all the rest controls relative to this textview? The code is:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/regBackground"
    android:orientation="vertical" >

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/regBackground" >

        <TextView
            android:id="@+id/txtProductNameScreenD"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="18dp"
            android:layout_marginTop="10dp"
            android:text="@string/empty"
            android:textColor="@color/_black"
            android:textSize="16sp"
            android:textStyle="bold"
            android:lines="1"
            android:ellipsize="end"
            android:scrollHorizontally="true"
            android:singleLine="true" >
        </TextView>

        <ImageView
            android:id="@+id/imgProductBcgD"
            android:layout_width="300dp"
            android:layout_height="263dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="40dp"
            android:src="@drawable/frame_product_screen" >
        </ImageView>

        <ImageView
            android:id="@+id/imgProductScreenD"
            android:layout_width="250dp"
            android:layout_height="260dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="42dp"
            android:background="@color/_white" >
        </ImageView>

        <ImageView
            android:id="@+id/imgProductScreendiscD"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top|center"
            android:layout_marginTop="205dp"
            android:src="@drawable/discount_bkg_product_screen" >
        </ImageView>

        <TextView
            android:id="@+id/txtProdScreenPrice"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right|top"
            android:layout_marginRight="45dp"
            android:layout_marginTop="220dp"
            android:ellipsize="end"
            android:lines="1"
            android:scrollHorizontally="true"
            android:singleLine="true"
            android:text="@string/empty"
            android:textColor="@color/_black"
            android:textSize="18sp" >
        </TextView>

        <TextView
            android:id="@+id/txtPriceShekelD1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right|top"
            android:layout_marginRight="80dp"
            android:layout_marginTop="230dp"
            android:ellipsize="end"
            android:lines="1"
            android:scrollHorizontally="true"
            android:singleLine="true"
            android:text="@string/shekel_symbol"
            android:textColor="@color/_gray"
            android:textSize="10sp" />

        <TextView
            android:id="@+id/txtBeforeDiscD"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right|top"
            android:layout_marginRight="25dp"
            android:layout_marginTop="237dp"
            android:ellipsize="end"
            android:lines="1"
            android:scrollHorizontally="true"
            android:singleLine="true"
            android:text="@string/before_discount"
            android:textColor="@color/_gray"
            android:textSize="10sp" />

        <TextView
            android:id="@+id/txtProdScreenDiscount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right|top"
            android:layout_marginRight="140dp"
            android:layout_marginTop="220dp"
            android:ellipsize="end"
            android:lines="1"
            android:scrollHorizontally="true"
            android:singleLine="true"
            android:text="@string/empty"
            android:textColor="@color/_orange"
            android:textSize="18sp" >
        </TextView>

        <TextView
            android:id="@+id/txtDiscountD"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right|top"
            android:layout_marginRight="145dp"
            android:layout_marginTop="237dp"
            android:ellipsize="end"
            android:lines="1"
            android:scrollHorizontally="true"
            android:singleLine="true"
            android:text="@string/bonus"
            android:textColor="@color/_gray"
            android:textSize="10sp" />

        <TextView
            android:id="@+id/txtProdScreenDiscountPrice"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left|top"
            android:layout_marginLeft="45dp"
            android:layout_marginTop="220dp"
            android:ellipsize="end"
            android:lines="1"
            android:scrollHorizontally="true"
            android:singleLine="true"
            android:text="@string/empty"
            android:textColor="@color/_black"
            android:textSize="18sp" >
        </TextView>

        <TextView
            android:id="@+id/txtPriceShekelD2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left|top"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="230dp"
            android:ellipsize="end"
            android:lines="1"
            android:scrollHorizontally="true"
            android:singleLine="true"
            android:text="@string/shekel_symbol"
            android:textColor="@color/_gray"
            android:textSize="10sp" />

        <TextView
            android:id="@+id/txtAfterDiscD"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left|top"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="237dp"
            android:ellipsize="end"
            android:lines="1"
            android:scrollHorizontally="true"
            android:singleLine="true"
            android:text="@string/after_discount"
            android:textColor="@color/_gray"
            android:textSize="10sp" />

        <Button
            android:id="@+id/btnShareFacebookD"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="255dp"
            android:background="@drawable/facebook_button_product_screen" >
        </Button>

        <TextView
            android:id="@+id/txtDatePSDiscD"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right|center_horizontal"
            android:layout_marginRight="90dp"
            android:layout_marginTop="315dp"
            android:text="@string/exp_date"
            android:textColor="@color/_gray"
            android:textSize="14sp" >
        </TextView>

        <TextView
            android:id="@+id/txtDateProductScreenD"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_horizontal"
            android:layout_marginLeft="90dp"
            android:layout_marginTop="315dp"
            android:text="@string/empty"
            android:textColor="@color/_blue"
            android:textSize="14sp" >
        </TextView>

        <ImageView
            android:id="@+id/imgSeparatorProductScreenD"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="345dp"
            android:src="@drawable/separator_product_screen" >
        </ImageView>

        <TextView
            android:id="@+id/txtDescriptionProductScreenD"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="355dp"
            android:inputType="textMultiLine"
            android:text="@string/empty"
            android:textColor="@color/_black"
            android:textSize="12sp" >
        </TextView>
    </FrameLayout>

</LinearLayout>

I can constrain the text to be just single line with the following parameter

android:singleLine = "true"

Moreover, you can fade the edge of it, just to make it look better

android:fadeScrollbars = "true"

If you want the users to be able to read the whole text you can add a scrolling effect. http://developer.android.com/reference/android/widget/TextView.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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