【问题标题】:Android ImageView in right of TextView with layout_centerHorizontal="true"Android ImageView 在 TextView 右侧,layout_centerHorizo​​ntal="true"
【发布时间】:2016-02-05 11:56:44
【问题描述】:

我有带有 android android:layout_centerHorizontal="true" 的 TextView,用户昵称在哪里,在 TextView 的右侧。我想添加一个 ImageView,在其中设置用户的状态(红色或绿色)但状态图像仍然在左边(layout_toEndOf="@+id/username" 已设置)。

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="@string/username"
    android:id="@+id/username"
    android:layout_alignParentTop="false"
    android:textColor="#921090"
    android:textSize="20dp"
    android:layout_centerHorizontal="true"/>

<ImageView
    android:layout_width="10dp"
    android:layout_height="10dp"
    android:id="@+id/status"
    android:layout_toEndOf="@+id/username"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/username"/>

【问题讨论】:

  • android:layout_toLeftOf="@id/status" 添加此 Textview 部分
  • 改用线性布局。你的问题会解决的
  • 尝试从ImageView中删除layout_alignParentTop
  • 放置屏幕图像以及您想要如何制作它。所以我们可以想象你想做什么?
  • 您尝试android:drawableRightandroid:drawableEnd 与textview?它会显示小的图像视图。

标签: android android-layout android-imageview


【解决方案1】:

请试试这个。

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">

        <TextView
            android:id="@+id/username"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="name"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#921090"
            android:textSize="20dp" />

        <ImageView
            android:id="@+id/status"
            android:layout_width="10dp"
            android:layout_height="10dp"
            android:layout_marginLeft="10dp" />

</LinearLayout>

【讨论】:

  • screencloud.net/v/DPBm android:layout_centerInParent="true" 它不是来自那里!我需要使用 RelativeLayout 因为布局更复杂,它是一个
  • 这是什么?? @FedotSerghei
  • 你到底想要什么?添加它的粗略图片。
  • 好的。现在请检查我的答案。 @FedotSerghei
【解决方案2】:

已尝试使用android:weightSum,因此所有具有不同分辨率的设备都将支持它。

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

    <TextView
        android:id="@+id/username"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="@string/username"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#921090"
        android:textSize="20dp" />

    <ImageView
        android:id="@+id/status"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/cal" />

</LinearLayout>

【讨论】:

    【解决方案3】:

    这是你的解决方案

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
        <TextView
            android:id="@+id/username"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="false"
            android:text="username"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#921090"
            android:paddingEnd="10dp"
            android:textSize="20dp" />
    
        <ImageView
            android:id="@+id/status"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_alignParentTop="true"
            android:layout_toEndOf="@+id/username"
            android:src="@drawable/ic_launcher" />
    
    </RelativeLayout>
    

    希望你觉得这很有用,......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-04
      • 1970-01-01
      • 2010-12-28
      • 1970-01-01
      • 1970-01-01
      • 2012-11-09
      • 1970-01-01
      相关资源
      最近更新 更多