简体   繁体   English

Android-如何相对于另一个控件设置边距?

[英]Android - how to set margin relative to another control?

I'm really new to android and would appreciate any help from you. 我真的是android的新手,不胜感激。 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. 每个模板都是一个单独的xml文件,我在其中放置了一堆控件。 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. 问题是我不知道上面的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? 有什么方法可以拉伸上方的textview,同时保持所有其余控件相对于此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 http://developer.android.com/reference/android/widget/TextView.html

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

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