在RelativeLayout中使用重力对齐TextView(Right align TextView using gravity in RelativeLayout)

编程入门 行业动态 更新时间:2024-10-26 19:40:24
在RelativeLayout中使用重力对齐TextView(Right align TextView using gravity in RelativeLayout)

我试图让editText坐在我的布局的右边缘,左边有一个按钮,如下所示:

<RelativeLayout android:id="@+id/FindClientLayout" android:layout_width="@dimen/third_width" android:layout_height="@dimen/half_height" android:layout_alignParentLeft="false" android:layout_alignParentRight="true" android:layout_alignParentTop="false" android:layout_below="@id/HeaderLayout" android:layout_margin="@dimen/fragment_separation" android:layout_toRightOf="@id/scrollViewRecommend" android:background="@drawable/login_border" > <TextView android:id="@+id/textViewFindClient" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="@string/find_client" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="@color/Black" /> <EditText android:id="@+id/editTextClient" android:layout_width="@dimen/input_text_width" android:layout_height="wrap_content" android:gravity="center_vertical|right" android:inputType="text"/> <Spinner android:id="@+id/spinnerClientList" android:layout_width="@dimen/input_text_width" android:layout_height="wrap_content" android:layout_below="@id/editTextClient"/> <Button android:id="@+id/buttonDoFind" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" android:layout_toLeftOf="@id/editTextClient" android:text="@string/find" /> </RelativeLayout>

该按钮不可见,结果如下所示:

我假设按钮是不可见的,因为editText是左对齐的,并覆盖它或将它从布局框架中推出。 为什么editText和spinner左对齐而不是右对齐? 另外,为什么editText不是垂直居中?

I am trying to get an editText to sit on the right edge of my layout, with a button to its left, as follows:

<RelativeLayout android:id="@+id/FindClientLayout" android:layout_width="@dimen/third_width" android:layout_height="@dimen/half_height" android:layout_alignParentLeft="false" android:layout_alignParentRight="true" android:layout_alignParentTop="false" android:layout_below="@id/HeaderLayout" android:layout_margin="@dimen/fragment_separation" android:layout_toRightOf="@id/scrollViewRecommend" android:background="@drawable/login_border" > <TextView android:id="@+id/textViewFindClient" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="@string/find_client" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="@color/Black" /> <EditText android:id="@+id/editTextClient" android:layout_width="@dimen/input_text_width" android:layout_height="wrap_content" android:gravity="center_vertical|right" android:inputType="text"/> <Spinner android:id="@+id/spinnerClientList" android:layout_width="@dimen/input_text_width" android:layout_height="wrap_content" android:layout_below="@id/editTextClient"/> <Button android:id="@+id/buttonDoFind" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" android:layout_toLeftOf="@id/editTextClient" android:text="@string/find" /> </RelativeLayout>

The button is invisible, and the result looks like this:

I assume the button is invisible because the editText is left-aligned and overlays it or pushes it out of the layout frame. Why is are the editText and spinner left aligned rather than right-aligned? Also, why is the editText not centered vertically?

最满意答案

像这样编辑你的xml文件并检查结果

<TextView android:id="@+id/textViewFindClient" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="@string/find_client" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="@color/Black" /> <EditText android:id="@+id/editTextClient" android:layout_width="@dimen/input_text_width" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:inputType="text"/> <Spinner android:id="@+id/spinnerClientList" android:layout_width="@dimen/input_text_width" android:layout_height="wrap_content" android:layout_below="@id/editTextClient"/> <Button android:id="@+id/buttonDoFind" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" android:layout_toLeftOf="@id/editTextClient" android:text="@string/find" />

这应该解决您的问题,即您正在使用重力将edittext置于右侧,但重力实际上用于定位视图的内容而不是视图本身。

如果你仍然面临问题留下评论

Edit you xml file like this and check for result

<TextView android:id="@+id/textViewFindClient" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="@string/find_client" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="@color/Black" /> <EditText android:id="@+id/editTextClient" android:layout_width="@dimen/input_text_width" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:inputType="text"/> <Spinner android:id="@+id/spinnerClientList" android:layout_width="@dimen/input_text_width" android:layout_height="wrap_content" android:layout_below="@id/editTextClient"/> <Button android:id="@+id/buttonDoFind" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" android:layout_toLeftOf="@id/editTextClient" android:text="@string/find" />

this should solve your problem which is , you are using gravity to place the edittext on right but gravity is actually use to position the content of view not the view itself.

If you still face problem leave a comment

更多推荐

本文发布于:2023-08-04 19:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1420683.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重力   RelativeLayout   TextView   gravity   align

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!