如何将图像视图和TexView左右对齐?

时间:2018-03-31 15:35:29

标签: java android android-layout layout

下面是我的布局文件。但是我无法将Textview(@ id / role)和Imageview(@ id / flag)左右对齐。文本似乎在ImageView上。

下面是我的布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >
    <ImageView
        android:id="@+id/flag"
        android:layout_margin="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:src="@drawable/bas" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp"
        android:orientation="vertical">


        <TextView
            android:id="@+id/name"
            android:text="Xyz Nepal"
            android:padding="5dp"
            android:layout_margin="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/red"
            android:textStyle="bold"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/role"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_marginTop="15dp"
            android:layout_toLeftOf="@id/flag"
            android:padding="5dp"
            android:text=" Nepali physicist specializing in cosmology. Having worked in the Central Department of Physics for 34 years"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#027e76"
            android:textStyle="bold" />

        <TextView
            android:layout_marginTop="15dp"
            android:id="@+id/desc"
            android:layout_margin="5dp"
            android:padding="5dp"
            android:text="Binil Aryal is the Professor and newly appointed Head of the Department at Central Department of Physics, Tribhuvan University, Kathmandu, Nepal.He was Born in 1969-09-28 in Saptari District of Eastern Nepal.
He is one of the actively working astrophysicist of Nepal. He has been working in the field of Galaxy orientation and Evolution. He has served as a judge of International Astronomy Olympiad. He is the country representative of BCVSPIN school.

He was awarded by 'Mahendra Bidhya Bhushan ka' by the late king Birendra Shah for securing highest score in the M.Sc. Examination. He received 'Third World Academy Award (TWAS)' for his Ph.D. work."
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#000000" />

        <TextView
            android:padding="5dp"
            android:layout_margin="5dp"
            android:id="@+id/email"
            android:text="xyz@gmail.com"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#0d38f7"/>

    </LinearLayout>


</RelativeLayout>

这是它出现的方式

enter image description here

这就是我想要的

enter image description here

先谢谢。我确实使用了Linearlayou和Relativelayout来定位项目,但是我做了一个糟糕的工作,而且我想延伸textview而不管屏幕尺寸如何,但不要重叠图像,而alwasy留在Image左边。

4 个答案:

答案 0 :(得分:1)

尝试此代码,看看它是否符合您的要求。

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:padding="5dp"
    android:text="@string/xyz"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textStyle="bold" />

<ImageView
    android:id="@+id/flag"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_alignParentRight="true"
    android:layout_margin="5dp"
    android:src="@drawable/launcher"
    android:layout_alignParentEnd="true" />

<TextView
    android:id="@+id/role"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:layout_marginTop="15dp"
    android:padding="5dp"
    android:layout_below="@id/name"
    android:layout_toLeftOf="@id/flag"
    android:text="@string/role
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="#027e76"
    android:textStyle="bold" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_below="@id/role"
    android:padding="10dp">


    <TextView
        android:id="@+id/desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_marginTop="15dp"
        android:padding="5dp"
        android:text="@string/desc"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:padding="5dp"
        android:text="xyz@gmail.com"
        android:textColor="#0d38f7" />

</LinearLayout>

答案 1 :(得分:1)

我想建议将带有id的文本视图作为名称,角色应位于单独的LinearLayout内,并进行调整以满足所需的UI。

     <?xml version="1.0" encoding="utf-8"?>
     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="horizontal">
     <ImageView
        android:id="@+id/flag"
        android:layout_margin="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:src="@drawable/bas" />

    <LinearLayout
        android:id="@+id/layout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:layout_toLeftOf="@+id/flag"
        android:orientation="vertical">



        <TextView
            android:id="@+id/name"
            android:text="Xyz Nepal"
            android:padding="5dp"
            android:layout_margin="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/red"
            android:textStyle="bold"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/role"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_marginTop="15dp"
            android:layout_toLeftOf="@id/flag"
            android:padding="5dp"
            android:text=" Nepali physicist specializing in cosmology. Having worked in the Central Department of Physics for 34 years"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#027e76"
            android:textStyle="bold" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/layout2"
        android:layout_below="@+id/layout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp"
        android:orientation="vertical">

         <TextView
            android:layout_marginTop="15dp"
            android:id="@+id/desc"
            android:layout_margin="5dp"
            android:padding="5dp"
            android:text="Binil Aryal is the Professor and newly appointed Head of the Department at Central Department of Physics, Tribhuvan University, Kathmandu, Nepal.He was Born in 1969-09-28 in Saptari District of Eastern Nepal.
He is one of the actively working astrophysicist of Nepal. He has been working in the field of Galaxy orientation and Evolution. He has served as a judge of International Astronomy Olympiad. He is the country representative of BCVSPIN school.

He was awarded by 'Mahendra Bidhya Bhushan ka' by the late king Birendra Shah for securing highest score in the M.Sc. Examination. He received 'Third World Academy Award (TWAS)' for his Ph.D. work."
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#000000" />

          <TextView
             android:padding="5dp"
             android:layout_margin="5dp"
            android:id="@+id/email"
            android:text="xyz@gmail.com"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#0d38f7"/>

    </LinearLayout>


</RelativeLayout>

答案 2 :(得分:1)

尝试以下方法:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<ImageView
    android:id="@+id/flag"
    android:layout_margin="5dp"
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:layout_alignParentRight="true"
    android:src="@drawable/capture" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:id="@+id/ll"
    android:layout_toStartOf="@id/flag"
    android:orientation="vertical">


    <TextView
        android:id="@+id/name"
        android:text="Xyz Nepal"
        android:padding="5dp"
        android:layout_margin="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/holo_red_light"
        android:textStyle="bold"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/role"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_marginTop="15dp"
        android:layout_toLeftOf="@id/flag"
        android:padding="5dp"
        android:text=" Nepali physicist specializing in cosmology. Having worked in the Central Department of Physics for 34 years"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#027e76"
        android:textStyle="bold" />

    </LinearLayout>

        <TextView
        android:layout_marginTop="15dp"
        android:id="@+id/desc"
        android:layout_margin="5dp"
        android:padding="15dp"
        android:text="Binil Aryal is the Professor and newly appointed Head     of the Department at Central Department of Physics, Tribhuvan University, Kathmandu, Nepal.He was Born in 1969-09-28 in Saptari District of Eastern Nepal.
He is one of the actively working astrophysicist of Nepal. He has been working in the field of Galaxy orientation and Evolution. He has served as a judge of International Astronomy Olympiad. He is the country representative of BCVSPIN school.

He was awarded by 'Mahendra Bidhya Bhushan ka' by the late king Birendra Shah for securing highest score in the M.Sc. Examination. He received 'Third World Academy Award (TWAS)' for his Ph.D. work."
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/ll"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#000000" />

    <TextView
        android:padding="15dp"
        android:layout_margin="5dp"
        android:id="@+id/email"
        android:text="xyz@gmail.com"
        android:layout_below="@id/desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#0d38f7"/>

    </RelativeLayout>

输出:

enter image description here

答案 3 :(得分:0)

使用垂直线性布局(默认为垂直)并将图像视图和文本视图(第一个文本视图example-xyz nepal)放入其中。并以其他文本视图的线性水平布局(示例 - 绿色文本和其他),它肯定会解决您的问题