Android LinearLayout中的ImageView显示在错误的位置

Android LinearLayout中的ImageView显示在错误的位置,android,imageview,android-linearlayout,Android,Imageview,Android Linearlayout,我的布局只有一行: 在源代码中,我只是设置了imageview的不同数量和可见性(GONE/VISIBLE)(单击不同的按钮) 因此: 图像不可见(已单击按钮1) 2.图像和一些长数字可激活(按钮2已单击) 3.图像和其他稍短的数字可见(按钮3) 你可以比较一下它的样子。文本变为可见,但图像位于前一位置。为什么?为什么图像不向左移动 顺便说一下。当屏幕旋转时-图像在正确的位置可见。尝试使用权重。。它将根据屏幕对齐 <LinearLayout android:layout_

我的布局只有一行:


在源代码中,我只是设置了imageview的不同数量和可见性(GONE/VISIBLE)(单击不同的按钮)

因此:

  • 图像不可见(已单击按钮1)
  • 2.图像和一些长数字可激活(按钮2已单击)

    3.图像和其他稍短的数字可见(按钮3)

    你可以比较一下它的样子。文本变为可见,但图像位于前一位置。为什么?为什么图像不向左移动


    顺便说一下。当屏幕旋转时-图像在正确的位置可见。

    尝试使用权重。。它将根据屏幕对齐

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:gravity="center_horizontal"
        android:orientation="horizontal" xmlns:android="http://schemas.android.com/apk/res/android">
    
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:paddingRight="@dimen/total_sales_page_summary_text_distance"
            android:text="test2"
            android:textSize="@dimen/total_sales_page_info_font_size" />
    
        <TextView
            android:id="@+id/year_on_year"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="@color/total_sales_info_text_color"
            android:textSize="@dimen/total_sales_page_info_font_size" />
    
        <ImageView
            android:id="@+id/tradeGreen"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_marginLeft="5dp"
            android:layout_gravity="center_vertical"
            android:contentDescription=""/>
    
    
    

    正如Deepchand正确地说的那样,将该图像直接添加到TextView中作为可绘制图像。这将考虑图像位置,并从布局中删除额外的ImageView

    因此,在您的文本视图中添加以下内容:

    <TextView
        android:id="@+id/year_on_year"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/total_sales_info_text_color"
        android:drawableRight="your image" //this will add an image to its right
        android:drawablePadding="5dp" // you can also add padding as per you need
        android:textSize="@dimen/total_sales_page_info_font_size" />
    
    
    

    希望这是你想要的方式。:)

    这将通过使用
    Weight
    解决

    在这里,您的UI是水平的,因此您需要在xml文件中进行更改。像

    <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:gravity="center_horizontal"
    android:orientation="horizontal" xmlns:android="http://schemas.android.com/apk/res/android">
    
    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:paddingRight="@dimen/total_sales_page_summary_text_distance"
        android:text="test2"
        android:textSize="@dimen/total_sales_page_info_font_size" />
    
    <TextView
        android:id="@+id/year_on_year"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:textColor="@color/total_sales_info_text_color"
        android:textSize="@dimen/total_sales_page_info_font_size" />
    
    <ImageView
        android:id="@+id/tradeGreen"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_marginLeft="5dp"
        android:layout_gravity="center_vertical"
        android:contentDescription=""/>
    
    </LinearLayout>
    

    您可以将图像用作textview drawableright,并将textview的内容宽度设为wrap,int,int,int)