窗口小部件显示在左侧,甚至带有属性android:layout_gravity=";中心“水平”;
我正在开发一个应用程序,目前正在开发图形界面。我希望第一个图像视图位于屏幕中央。我使用了属性窗口小部件显示在左侧,甚至带有属性android:layout_gravity=";中心“水平”;,android,android-layout,center,android-xml,layout-gravity,Android,Android Layout,Center,Android Xml,Layout Gravity,我正在开发一个应用程序,目前正在开发图形界面。我希望第一个图像视图位于屏幕中央。我使用了属性android:layout\u gravity=“center\u horizontal”,但图像显示在屏幕左侧 是因为我定义了第二个LinearLayout,其中包含ImageView和textViews吗?我定义它是为了定义文本元素的权重和包含按钮的水平LinearLayout的权重 <?xml version="1.0" encoding="utf-8"?> <LinearLay
android:layout\u gravity=“center\u horizontal”
,但图像显示在屏幕左侧
是因为我定义了第二个LinearLayout
,其中包含ImageView
和textView
s吗?我定义它是为了定义文本元素的权重和包含按钮的水平LinearLayout
的权重
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="4"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:src="@drawable/ebay"/> <!-- a modifier plus tard -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_gravity="left"
android:text="Book : "
android:textSize="20sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="Sea change"
android:textSize="25sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_gravity="left"
android:text="ISBN"
android:textSize="20sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="246546524"
android:textSize="30sp"/>
</LinearLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="OK"/>
</LinearLayout>
那么我怎样才能使其位于屏幕中央?将您的第二个版面宽度从
wrap\u content
更改为match\u parent
:
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="4"
android:gravity="center_horizontal"
android:orientation="vertical">
这是您的xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="4"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:src="@drawable/ebay" />
<!-- a modifier plus tard -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginTop="20dp"
android:text="Book : "
android:textSize="20sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="Sea change"
android:textSize="25sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginTop="20dp"
android:text="ISBN"
android:textSize="20sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="246546524"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="OK" />
</LinearLayout>
这里是输出屏幕:您在这里谈论的是哪种线性布局,这里有太多。您不应该试图将您的问题更改为答案。您可以通过添加答案来回答自己的问题。@UditKapahi:我说的是第二个线性布局,它是水平的,然后只需更改布局宽度以填充\u parentNo,它不起作用,ImageView仍然在左侧是的,您是对的,可能更改了其他属性。。现在它工作了,我复制了你的代码!谢谢