java 项目在 Android Studio 模拟器中的位置错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42724144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Items are in wrong place in Android Studio emulator
提问by A. Sharkh
I'm new to android studio and this is my first app. It works correctly but having an issue with the emulator, it shows the button in the wrong place.
我是 android studio 的新手,这是我的第一个应用程序。它工作正常,但模拟器出现问题,它在错误的位置显示按钮。
<android.support.constraint.ConstraintLayout 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"
tools:context="com.example.dell_pc.myapplication.MainActivity">
<EditText
android:id="@+id/txtyear"
android:layout_width="193dp"
android:layout_height="60dp"
android:ems="10"
android:hint="Enter year of birth"
android:inputType="number"
tools:layout_editor_absoluteY="129dp"
tools:layout_editor_absoluteX="96dp" />
<Button
android:id="@+id/butage"
android:layout_width="115dp"
android:layout_height="41dp"
android:onClick="BuGetAge"
android:text="Show Age"
tools:layout_editor_absoluteY="277dp"
tools:layout_editor_absoluteX="135dp" />
</android.support.constraint.ConstraintLayout>
How can I make it to appear in the right place?
我怎样才能让它出现在正确的地方?
采纳答案by Aimy_N_Chu
Do you absolutely need to use constraint layout? If not, you can use a relative layout, and use layout_centerHorizontal and layout_centerVertical like this:
你绝对需要使用约束布局吗?如果没有,您可以使用相对布局,并像这样使用 layout_centerHorizontal 和 layout_centerVertical:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<EditText
android:id="@+id/edittext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="blah blah"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="press"
android:layout_centerHorizontal="true"
android:layout_below="@id/edittext"
/>
</RelativeLayout>
回答by Parman Josan
Select Component/Item(such as BUTTON) in activity_main.xml -> click on "Infer Constrains(as I show in attached file)"
在activity_main.xml 中选择组件/项目(例如按钮)-> 单击“推断约束(如我在附件中所示)”
Your item will take right place in ASE
您的物品将在 ASE 中正确放置
Do same for the rest Component/Items
对其余组件/项目执行相同操作
回答by Okas
tools:layout_editor_absoluteY and other attributes from tools namespace only have effect in layout editor. Your working code does not use these attributes, so your views are in upper left corner, because this is default. As noted above RelativeLayout or even LinearLayout suits better such simple layout.
tools:layout_editor_absoluteY 和来自工具命名空间的其他属性仅在布局编辑器中有效。您的工作代码不使用这些属性,因此您的视图位于左上角,因为这是默认设置。如上所述,RelativeLayout 甚至 LinearLayout 更适合这种简单的布局。
回答by hev1
回答by Rzouga Younsi
Just change:
只是改变:
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
to
到
</RelativeLayout>
</RelativeLayout>
Problem solved! Be happy :D
问题解决了!开心点 :D