相对布局android中的文本对齐方式

时间:2014-04-07 09:50:56

标签: android layout

我试图动态地在我的Listview内膨胀相对布局,但是我无法根据上面的ImageView包装文本内容。请帮忙整理一下。

我的相对布局代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/background_gradient"
    android:orientation="vertical"
    android:paddingRight="5dp" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/img1" />

    <TextView
        android:id="@+id/title_course"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@+id/imageView1"
        android:layout_below="@+id/imageView1"
        android:background="#08298A"
        android:text="This is my text view in android example"
        android:textColor="#FFFFFF" />

</RelativeLayout>

我的自定义列表视图代码如下:在线性布局id my_linear

内膨胀相对布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/margin_small" >

    <TextView
        android:id="@+id/title_curriculum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:paddingLeft="5dp" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="5dp"
        android:contentDescription="@string/app_name"
        android:paddingRight="5dp"
        android:src="@drawable/progress_bar" />
</RelativeLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/background_gradient"
    android:layout_margin="@dimen/margin_small" >

    <ImageView
        android:id="@+id/course_info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/app_name"
        android:src="@drawable/ic_info_scroller" />

    <HorizontalScrollView
        android:layout_marginLeft="5dp"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:scrollbars="none" >

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

5 个答案:

答案 0 :(得分:0)

false XML中的android:singleLine设置为TextView,如下所示......

android:singleLine="false"

因此,更新的布局XML将是......

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/background_gradient"
    android:orientation="vertical"
    android:paddingRight="5dp" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/img1" />

    <TextView
        android:id="@+id/title_course"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@+id/imageView1"
        android:layout_below="@+id/imageView1"
        android:background="#08298A"
        android:singleLine="false"
        android:text="This is my text view in android example"
        android:textColor="#FFFFFF" />
</RelativeLayout>

答案 1 :(得分:0)

您可以尝试将TextView粘贴在自己的LinearLayout中。

答案 2 :(得分:0)

<ImageView
    android:id="@+id/img"
    android:layout_width="wrap_content"
    android:src="@android:drawable/gallery_thumb"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/img"
    android:layout_alignLeft="@+id/img"
    android:layout_alignRight="@+id/img"
    android:text="This dummy text here testttttttttt"
    android:textAppearance="?android:attr/textAppearanceLarge" />

答案 3 :(得分:0)

<TextView
android:id="@+id/title_course"
android:layout_width="**match_parent**"
android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_below="@+id/imageView1"
android:background="#08298A"
android:text="This is my text view in android example"
android:textColor="#FFFFFF" />

您要在宽度上包装内容,因此父级宽度无关紧要。使用match_parent,它应该有效。我还删除了parentBottom和alignRight,不确定它是否可行。

答案 4 :(得分:0)

我在android:width="0dp"中尝试了TextView并获得了所需的输出。