我有一行布局:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
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="match_parent"
android:layout_height="wrap_content"
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="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_gravity="center_vertical"
android:contentDescription=""/>
</LinearLayout>
在源代码中,我只设置了imageview的不同数量和可见性(GONE / VISIBLE)(点击不同的按钮)。
结果:
2.image和一些长数字是可生效的(按钮2被点击)
3.可以看到图像和其他较短的数字(按钮3)
您可以比较它的外观。文本变为可见但图像位于上一个位置。为什么?为什么图像不会向左移动。
顺便说一下。旋转屏幕时 - 图像在正确的位置可见。答案 0 :(得分:1)
尝试使用重量..它将根据屏幕对齐
<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=""/>
答案 1 :(得分:0)
正如Deepchand正确地说的那样,将Image作为drawable直接添加到TextView中。这将处理图像位置,并将从布局中删除额外的ImageView。
所以对你的TextView添加这个,
<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" />
希望这可以按照你想要的方式运作。 :)
答案 2 :(得分:0)
这将通过使用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>