android 工作室从右到左
Right to left in android studio
我正在使用 Android Studio 进行应用程序开发,但在屏幕中放置元素时遇到问题。
例如:我试图在屏幕右侧放置一个按钮,在工作室中它显示在屏幕右侧,但是当我在智能手机中安装该应用程序时,我看到它在屏幕左侧屏幕(我的智能手机是从右到左配置的)。
我该如何解决? (如果重要的话,我使用 RelativeLayout
)。
谢谢!
illustration image
申请者代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"><![CDATA[
android:splitMotionEvents="true"
tools:context="tomer.newapp.MainActivity"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
]]>
<Button
android:id="@+id/btn"
android:layout_width="145dp"
android:layout_height="75dp"
android:layout_column="2"
android:layout_row="2"
android:onClick="btnclick"
android:text="Click!"
android:textSize="30sp"
tools:layout_editor_absoluteX="223dp"
tools:layout_editor_absoluteY="140dp"
android:layout_marginLeft="11dp"
android:layout_marginStart="11dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
如果您包含一些代码,回答您的问题会更容易。但是,假设您的按钮位于填满屏幕的相对父布局内,将此添加到按钮的 xml 代码将使它向右对齐:
android:layout_alignParentRight="true"
即
<Button
android:id="@+id/btn"
android:layout_width="145dp"
android:layout_height="75dp"
android:layout_alignParentRight="true"
android:onClick="btnclick"
android:text="Click!"
android:textSize="30sp" />
或者为了让它变得非常简单,将整个 XML 替换为:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<Button
android:id="@+id/btn"
android:layout_width="145dp"
android:layout_height="75dp"
android:onClick="btnclick"
android:text="Click!"
android:textSize="30sp"
android:layout_alignParentRight="true"/>
</RelativeLayout>
请注意,您使用的绝对值位于 tools
命名空间中 - 这意味着它们不会编译到您的应用程序中。
tools:layout_editor_absoluteX="223dp"
tools:layout_editor_absoluteY="140dp"
这可能就是您出现这种行为的原因。
删除它们,然后查看按钮是否移动到屏幕左侧。
有关 tools
命名空间的更多信息 - here
您提到“(我的智能手机是从右到左配置的)”。
这可能是一个愚蠢的答案,但是...
对于 layout_editor,对于 "gravity" 或 "layout_gravity" 属性,存在 "right"、"left" 或 "start"和 "end"。在英语中,一个人从左边开始写作,在右边结束。例如,在阿拉伯语中,一个从右边开始到左边结束。有没有可能你的 属性 说 "end" 而不是 "right"?
我正在使用 Android Studio 进行应用程序开发,但在屏幕中放置元素时遇到问题。
例如:我试图在屏幕右侧放置一个按钮,在工作室中它显示在屏幕右侧,但是当我在智能手机中安装该应用程序时,我看到它在屏幕左侧屏幕(我的智能手机是从右到左配置的)。
我该如何解决? (如果重要的话,我使用 RelativeLayout
)。
谢谢!
illustration image
申请者代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"><![CDATA[
android:splitMotionEvents="true"
tools:context="tomer.newapp.MainActivity"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
]]>
<Button
android:id="@+id/btn"
android:layout_width="145dp"
android:layout_height="75dp"
android:layout_column="2"
android:layout_row="2"
android:onClick="btnclick"
android:text="Click!"
android:textSize="30sp"
tools:layout_editor_absoluteX="223dp"
tools:layout_editor_absoluteY="140dp"
android:layout_marginLeft="11dp"
android:layout_marginStart="11dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
如果您包含一些代码,回答您的问题会更容易。但是,假设您的按钮位于填满屏幕的相对父布局内,将此添加到按钮的 xml 代码将使它向右对齐:
android:layout_alignParentRight="true"
即
<Button
android:id="@+id/btn"
android:layout_width="145dp"
android:layout_height="75dp"
android:layout_alignParentRight="true"
android:onClick="btnclick"
android:text="Click!"
android:textSize="30sp" />
或者为了让它变得非常简单,将整个 XML 替换为:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<Button
android:id="@+id/btn"
android:layout_width="145dp"
android:layout_height="75dp"
android:onClick="btnclick"
android:text="Click!"
android:textSize="30sp"
android:layout_alignParentRight="true"/>
</RelativeLayout>
请注意,您使用的绝对值位于 tools
命名空间中 - 这意味着它们不会编译到您的应用程序中。
tools:layout_editor_absoluteX="223dp"
tools:layout_editor_absoluteY="140dp"
这可能就是您出现这种行为的原因。 删除它们,然后查看按钮是否移动到屏幕左侧。
有关 tools
命名空间的更多信息 - here
您提到“(我的智能手机是从右到左配置的)”。
这可能是一个愚蠢的答案,但是...
对于 layout_editor,对于 "gravity" 或 "layout_gravity" 属性,存在 "right"、"left" 或 "start"和 "end"。在英语中,一个人从左边开始写作,在右边结束。例如,在阿拉伯语中,一个从右边开始到左边结束。有没有可能你的 属性 说 "end" 而不是 "right"?