Android 如何在纵向和横向上使用中心上的6个按钮居中线性布局?

Android 如何在纵向和横向上使用中心上的6个按钮居中线性布局?,android,Android,我想要实现的是:两个方向的中心布局 <ScrollView android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:background="#fffffce0"> <RelativeLayout android

我想要实现的是:两个方向的中心布局

    <ScrollView
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#fffffce0">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#fffffce0"
    android:weightSum="1">


    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="121dp"
        android:background="#fffffce0"
        android:layout_marginTop="42dp"
        android:id="@+id/linearLayout"
        android:gravity="center_horizontal"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <Button
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:id="@+id/button9"
            android:background="@drawable/custom_button_square"
            android:text="@string/a_d"
            android:textColor="#ffff"
            android:textSize="35sp"
            android:onClick="goToAd"/>

        <Button
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:id="@+id/button10"
            android:background="@drawable/custom_button_square"
            android:text="@string/e_h"
            android:textColor="#ffff"
            android:textSize="35sp" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:layout_below="@+id/linearLayout"
        android:layout_centerHorizontal="true"
        android:gravity="center_horizontal"
        android:background="#fffffce0"
        android:id="@+id/linearLayout2">

        <Button
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:id="@+id/button11"
            android:background="@drawable/custom_button_square"
            android:text="@string/i_l"
            android:textColor="#ffff"
            android:textSize="35sp" />

        <Button
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:id="@+id/button12"
            android:background="@drawable/custom_button_square"
            android:text="@string/m_p"
            android:textColor="#ffff"
            android:textSize="35sp" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:layout_below="@+id/linearLayout2"
        android:layout_centerHorizontal="true"
        android:background="#fffffce0"
        android:gravity="center_horizontal">

        <Button
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:id="@+id/button13"
            android:background="@drawable/custom_button_square"
            android:text="@string/m_p"
            android:textColor="#ffff"
            android:textSize="35sp" />

        <Button
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:id="@+id/button14"
            android:background="@drawable/custom_button_square"
            android:text="@string/q_t"
            android:textColor="#ffff"
            android:textSize="35sp" />
    </LinearLayout>

</RelativeLayout>

问题: 一切都很好,直到我在更大屏幕的平板电脑上测试我的应用程序的那一刻。当方向为横向时,一切正常,但当我旋转屏幕时,它看起来如下:


我试着把重心调到中间,但没用。

因为屏幕分辨率不同,试着不要使用像android:layout\u height=“70dp”这样的硬编码数字。您可以通过编程将布局的每个元素的布局参数分别设置为实际屏幕分辨率,在这种情况下,它将适用于每个屏幕大小和方向。试试这样的方法:

//declare two global variables that will have the width and height values
private int width;
private int height;

//define a method that will set the values to the both variables
private static void getScreenResolution(Context context){
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);
    width = metrics.widthPixels;
    height = metrics.heightPixels;
}
onCreate()
方法中,首先调用上述方法以获取值,然后设置布局参数(仅举一个简单的示例):


希望这能帮助您在将来创建更通用的设计:)

好吧!这样地。试试这个

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fffffce0" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fffffce0"
    android:gravity="center"
    android:layout_gravity="center"
    android:orientation="vertical"
    android:weightSum="1" >

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="121dp"
        android:layout_marginTop="42dp"
        android:background="#fffffce0"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button9"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:background="@drawable/ic_launcher"
            android:onClick="goToAd"
            android:text="a_d"
            android:textColor="#ffff"
            android:textSize="35sp" />

        <Button
            android:id="@+id/button10"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:background="@drawable/ic_launcher"
            android:text="e_h"
            android:textColor="#ffff"
            android:textSize="35sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:background="#fffffce0"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button11"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:background="@drawable/ic_launcher"
            android:text="i_l"
            android:textColor="#ffff"
            android:textSize="35sp" />

        <Button
            android:id="@+id/button12"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:background="@drawable/ic_launcher"
            android:text="m_p"
            android:textColor="#ffff"
            android:textSize="35sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:background="#fffffce0"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button13"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:background="@drawable/ic_launcher"
            android:text="m_p"
            android:textColor="#ffff"
            android:textSize="35sp" />

        <Button
            android:id="@+id/button14"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:background="@drawable/ic_launcher"
            android:text="q_t"
            android:textColor="#ffff"
            android:textSize="35sp" />
    </LinearLayout>
</LinearLayout>



尝试从
线性布局中的每个
视图中删除
android:layout\u gravity=“center\u horizontal”
,您可以发布整个XML吗?它看起来不完整。@VishwajitPalankar和@Faruk Yazıcı,我的坏我粘贴了错误的活动,现在被纠正了,不幸的是没有任何改变。这些都是对我的影响。你想要同样的吗?知道我手机上的问题吗?当我将方向更改为Landscape三星galaxy core 2时,顶部的2个按钮被切掉了,它向下滚动,但顶部的按钮被切掉了。我与其他布局一起检查,每次相同的问题,切掉的上部都知道我手机上的问题,当我将方向更改为Landscape创建dimens文件并使用dimens文件中的按钮高度值时,顶部的这2个按钮被剪切。或将按钮高度值设置为较小值,如50dp。谢谢@Ortoh
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fffffce0" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fffffce0"
    android:gravity="center"
    android:layout_gravity="center"
    android:orientation="vertical"
    android:weightSum="1" >

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="121dp"
        android:layout_marginTop="42dp"
        android:background="#fffffce0"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button9"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:background="@drawable/ic_launcher"
            android:onClick="goToAd"
            android:text="a_d"
            android:textColor="#ffff"
            android:textSize="35sp" />

        <Button
            android:id="@+id/button10"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:background="@drawable/ic_launcher"
            android:text="e_h"
            android:textColor="#ffff"
            android:textSize="35sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:background="#fffffce0"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button11"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:background="@drawable/ic_launcher"
            android:text="i_l"
            android:textColor="#ffff"
            android:textSize="35sp" />

        <Button
            android:id="@+id/button12"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:background="@drawable/ic_launcher"
            android:text="m_p"
            android:textColor="#ffff"
            android:textSize="35sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:background="#fffffce0"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button13"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:background="@drawable/ic_launcher"
            android:text="m_p"
            android:textColor="#ffff"
            android:textSize="35sp" />

        <Button
            android:id="@+id/button14"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:background="@drawable/ic_launcher"
            android:text="q_t"
            android:textColor="#ffff"
            android:textSize="35sp" />
    </LinearLayout>
</LinearLayout>
   <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fffffce0" >

<RelativeLayout android:id="@+id/layout" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"

    >
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#fffffce0"
    android:orientation="vertical"
    android:weightSum="3" >

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:background="#fffffce0"
        android:orientation="horizontal"
        android:weightSum="2" >

        <Button
            android:id="@+id/button9"
            android:layout_width="0dip"
            android:layout_height="120dp"
            android:layout_weight="1"
            android:background="@drawable/ic_launcher"
            android:onClick="goToAd"
            android:text="a_d"
            android:textColor="#ffff"
            android:textSize="35sp" />

        <Button
            android:id="@+id/button10"
            android:layout_width="0dip"
            android:layout_height="120dp"
            android:layout_weight="1"
            android:background="@drawable/ic_launcher"
            android:text="e_h"
            android:textColor="#ffff"
            android:textSize="35sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:background="#fffffce0"
        android:orientation="horizontal"
        android:weightSum="2" >

        <Button
            android:id="@+id/button11"
             android:layout_width="0dip"
            android:layout_height="120dp"
            android:layout_weight="1"
            android:background="@drawable/ic_launcher"
            android:text="i_l"
            android:textColor="#ffff"
            android:textSize="35sp" />

        <Button
            android:id="@+id/button12"
            android:layout_width="0dip"
            android:layout_height="120dp"
            android:layout_weight="1"
            android:background="@drawable/ic_launcher"
            android:text="m_p"
            android:textColor="#ffff"
            android:textSize="35sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:weightSum="2"
        android:background="#fffffce0"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button13"
            android:layout_width="0dip"
            android:layout_height="120dp"
            android:layout_weight="1"
            android:background="@drawable/ic_launcher"
            android:text="m_p"
            android:textColor="#ffff"
            android:textSize="35sp" />

        <Button
            android:id="@+id/button14"
            android:layout_width="0dip"
            android:layout_height="120dp"
            android:layout_weight="1"
            android:background="@drawable/ic_launcher"
            android:text="q_t"
            android:textColor="#ffff"
            android:textSize="35sp" />
    </LinearLayout>
</LinearLayout>