gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 14:08:23 26 4
gpt4 key购买 nike

我想要实现的目标:双向居中布局。

    <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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com