简体   繁体   中英

how to align textview according to above imageview in android

I m not satisfied with the result of my layout whenever I test it in landscape mode on different screen size devices. In portrait I like the margin of the textview. Since my current image size of the imageview will not fill the full width of some devices out there in landscape there is some whitespace left and right to it, which is fine. however, the margin of the textview should also take this whitespace into account and add it somehow. I know i can create a different xml style for landscape modes, but any fixed margin wont help here... thanks for any help in advance

在此输入图像描述

 <ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:scaleType="fitStart"
    android:adjustViewBounds="true"
    android:src="@drawable/start1_8" />

<TextView
    style="@style/DescriptionTitle"
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/imageView1"
    android:text="Test Titelzeile"   />

<style name="DescriptionTitle" parent="@android:style/TextAppearance">
    <item name="android:textColor">#0099cc</item>
    <item name="android:textStyle">bold|italic</item>
    <item name="android:textSize">20sp</item>
    <item name="android:layout_gravity">center_vertical|left</item>
    <item name="android:gravity">top|left</item>
    <item name="android:layout_marginLeft">15dp</item>
    <item name="android:layout_marginRight">15dp</item>
    <item name="android:layout_marginTop">5dp</item>

</style>

I would put it in a Relative Layout like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:gravity="center_horizontal"
              android:layout_height="match_parent">
    <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:scaleType="fitStart"
            android:adjustViewBounds="true"
            android:src="@drawable/Untitled-1" />
    <TextView
            style="@style/DescriptionTitle"
            android:id="@+id/textView1"
            android:layout_alignLeft="@id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/imageView1"
            android:text="Test Titelzeile"   />
</RelativeLayout>

Then it will be in the correct relative position regardless of view size or orientation.

resize the view in runtime and use(like screenwidth/4 ) a ratio hor image width and position.

use LayoutParams to do it.

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