android - 如何在纵向和横向方向上将带有 6 个按钮的线性布局居中?

标签 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>

问题: 一切都很好,直到我在更大屏幕的平板电脑上测试我的应用程序。当方向是横向时,一切都很好,但是当我旋转屏幕时,它看起来像这样: http://zapodaj.net/b2780f7e637ab.png.html

我试图将布局中的重力设置为居中,但没有成功。

最佳答案

由于屏幕分辨率不同,请尽量不要使用像 android:layout_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() 方法中,首先调用上面的方法来获取值,然后设置布局参数(只是一个简单的例子):

getScreenResolution(this); //if you are in a fragment, type getActivity() instead of this
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,                             LayoutParams.MATCH_PARENT);
layoutParams.setMargins(width/10, height/15, 0 ,0);   //left, top, right, bottom
yourLinearLayout.setLayoutParams(layoutParams);

希望这可以帮助您在未来创建更通用的设计:)

关于android - 如何在纵向和横向方向上将带有 6 个按钮的线性布局居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31070516/

相关文章:

java - 如果我这样做的话,android 中创建的文件会在哪里

android - 测试一个点是否在没有实际多边形对象的多边形中。 Android 谷歌地图 v2

android - 确定是否以编程方式启用系统范围的备份

android - 我可以使用 Android 应用程序来跟踪用户 Activity 吗?

Android根据宽度设置图片 View 高度

java - 无法证实 Activity ComponentInfo - 空指针异常

android - 元素使用权限#android.permission.CAMERA 与在 AndroidManifest.xml 中声明的元素重复

android - 使用 RxJava Async 时的循环推理

android - 我如何为三星银河构建我自己的机器人

JSONArray 中的 Android JSONArrays