android - 将 ImageView 和 TextView 放在同一行

标签 android xml android-layout

我有几个 Logo ,我想在它旁边设置一个 Textview,以便它们在同一行。我不能将它们放在同一条线上。请帮助我。我希望我的 TextView 紧挨着我的图像。

我的布局

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

    <!--
      <fragment 
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
    -->

    <TextView
        android:id="@+id/filmhall_contactdetails"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/cinema_filmhall_contactdet"
        android:textColor="#000000"
        android:layout_margin="4dp"
        android:toRightof="@+id/pointer"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/filmhall_address"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="32dp"
        android:layout_marginTop="2dp"
        android:text="@string/cinema_filmhall_address"
        android:textColor="#000000"
        android:textSize="12sp"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/pointer"
        android:layout_width="15dp"
        android:layout_height="15dp"
        android:layout_marginLeft="4dp"

        android:src="@drawable/pointer" />

    <TextView
        android:id="@+id/filmhall_telephone1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/cinema_filmhall_tp1"
        android:textColor="#000000"
         android:layout_marginTop="2dp"
         android:layout_marginLeft="32dp"
          android:textSize="12sp"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/telephone"
        android:layout_width="15dp"
        android:layout_height="15dp"
        android:layout_marginLeft="4dp"
        android:src="@drawable/call" />

    <TextView
        android:id="@+id/filmhall_telephone2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/cinema_filmhall_tp2"
        android:textColor="#000000"
         android:layout_marginTop="2dp"
         android:layout_marginLeft="32dp"
          android:textSize="12sp"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/fax"
        android:layout_width="15dp"
        android:layout_height="15dp"
        android:layout_marginLeft="4dp"
        android:src="@drawable/fax" />

    <TextView
        android:id="@+id/filmhall_email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/cinema_filmhall_email"
        android:textColor="#000000"
        android:layout_marginTop="2dp"
        android:layout_marginLeft="32dp"
         android:textSize="12sp"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/message"
        android:layout_width="15dp"
        android:layout_height="15dp"
        android:layout_marginLeft="4dp"
        android:src="@drawable/message" />

    <TextView
        android:id="@+id/filmhall_facility"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="4dp"
        android:layout_marginTop="10dp"
        android:text="@string/cinema_filmhall_facility"
        android:textColor="#000000"
        android:textStyle="bold" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
>


        <ImageView
            android:id="@+id/fac1"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginLeft="4dp"
            android:src="@drawable/fac1" />

        <ImageView
            android:id="@+id/fac3"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginLeft="4dp"
            android:src="@drawable/fac3" />

        <ImageView
            android:id="@+id/fac2"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginLeft="4dp"
            android:src="@drawable/fac2" />
    </LinearLayout>

</LinearLayout>

最佳答案

方法一:

像其他人建议的那样,您可以使用水平方向的嵌套 LinearLayout。它将包含 TextViewImageView,并根据您的需要设置权重。

参见 How to set ImageView and the textView in a single line inside a LinearLayout

例如:

 <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal" >

     <ImageView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_weight="0.5"
         ...
         />

     <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_marginLeft="10dp"
         android:layout_weight="0.5"
         android:text="Test2" />
</LinearLayout>

方法 2 [更简单]:

既然您提到您有多个 Logo 并且想在其旁边设置 drawable/ImageView,那么有一种更简单的替代方法。假设你想要这样的东西: enter image description here

您可以使用 android:drawableLeftTextView 中的类似内容。您还可以使用 android:drawablePadding 设置可绘制对象和 TextView 文本之间的填充。 .使用这种方式你不需要额外的 ImageView。但这最终取决于您要寻找的是什么。

参见 Programmatically set left drawable in a TextView

希望这对您有所帮助。

关于android - 将 ImageView 和 TextView 放在同一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25173720/

相关文章:

android - 向布局添加文本?

android - 如何在 Andengine Base 游戏 Activity 中使用 Ratio Resolution 策略时覆盖 onSetContentView

java - 如何将自定义数据和字段添加到Android的“联系人”屏幕?

java - 使用 JAXP(JEE6) 解析简单的 XML 文档

javascript - 如何在 WSO2 ESB 中创建全局变量 - 必须可更新

java - 在 Android 中创建秒表计时器

java - 返回值的可运行替代方案

java - 火力地堡 : How to search all objects with same value in whole database or nested database

php - 从 mysql 服务器生成用于 iphone 应用程序消费的 XML 不起作用

android - 为什么 findViewById(...) 返回 null?