【问题标题】:How can i make my layout resize proportionally as screen size changes?如何使我的布局随着屏幕尺寸的变化而按比例调整?
【发布时间】:2014-03-16 08:42:34
【问题描述】:

我有 9 行和 6 列按钮,所以我使用此代码按比例更改每个按钮的大小:

DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    height = displaymetrics.heightPixels;
    width = displaymetrics.widthPixels;

    int Orientation = getResources().getConfiguration().orientation;

    if(Orientation == Configuration.ORIENTATION_PORTRAIT)
    {
        Button btn1 = (Button) findViewById(R.id.button1);
        btn1.setLayoutParams(new LayoutParams(height/10,width/7));
        Button btn2 = (Button) findViewById(R.id.button2);
        btn2.setLayoutParams(new LayoutParams(height/10,width/7));
        Button btn3 = (Button) findViewById(R.id.button3);
        btn3.setLayoutParams(new LayoutParams(height/10,width/7));
        Button btn4 = (Button) findViewById(R.id.button4);
        btn4.setLayoutParams(new LayoutParams(height/10,width/7

但这不起作用按钮大小的变化,但它们不适合。下面是我的xml代码:

<?xml version="1.0" encoding="utf-8"?>

<ScrollView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" >

<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp" >

<TableRow
    android:id="@+id/tableRow1"
    android:weightSum="1" >

    <TextView
    android:id="@+id/ResultText"
    android:gravity="right"
    android:textSize="20sp"
    android:background="#AAAAAA"
    android:layout_weight="1"
    android:textColor="#FFFFFF"
     />

</TableRow>

<TableRow
    android:id="@+id/tableRow2"
    android:layout_width="fill_parent"
    android:weightSum="1" >

    <TextView
    android:id="@+id/InputText"
    android:background="#BBBBBB"
    android:gravity="right"
    android:textColor="#FFFFFF"
    android:layout_weight="1"
    android:layout_marginTop="4dp"
    android:textSize="15sp" />


</TableRow>

<TableRow
    android:id="@+id/tableRow0"
    android:layout_width="wrap_content"
    android:weightSum="1" >

    <TextView
    android:id="@+id/memoryStatText"
    android:background="#BBBBBB"
    android:gravity="right"
    android:textColor="#FFFFFF"
    android:layout_weight="1"
    android:layout_marginTop="4dp"
    android:textSize="15sp"
    android:layout_height="wrap_content" />

</TableRow>

<TableRow
    android:id="@+id/tableRow3"
    android:layout_width="wrap_content"
    android:weightSum="1" >

   <Button
    android:id="@+id/buttonMAdd"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp"
    android:layout_marginTop="10dp"
    android:layout_weight="0.166"     
    android:text="M+"
    android:onClick="ProcessInput"
    android:gravity="fill"/>

    <Button
    android:id="@+id/buttonMRemove"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp"
    android:layout_marginTop="10dp" 
    android:layout_weight="0.166"    
    android:text="M-"
    android:onClick="ProcessInput"
    android:gravity="fill" />

    <Button
    android:id="@+id/buttonMC"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp"
    android:layout_marginTop="10dp"
    android:layout_weight="0.166"     
    android:text="MC"
    android:onClick="ProcessInput"
    android:gravity="fill" />

在任何情况下,布局都可以在所有设备上完美地水平延伸,但在较大的设备上,应用程序底部仍有大量空间空置。为了改变高度,我将代码更改为:

Button btn1 = (Button) findViewById(R.id.button1);
btn1.setHeight(height/10));

但这会无缘无故地减少按钮的宽度。我做错了什么。我的问题基本上是我可以让我的布局垂直适合水平以及水平适合。

这是我在两种情况下的图像:

这是我的第二张图片:

【问题讨论】:

  • 尝试自定义 ViewGroup 来布局您的孩子
  • 我是一个新手,不知道它是如何工作的,你能给我一个链接到一些易于理解的教程吗?
  • google for:自定义视图组
  • 有没有其他方法,我在考虑创建三个或四个应用程序之后再创建自定义视图组。
  • 我不知道更好的

标签: android xml android-layout


【解决方案1】:

尝试将您的 TableLayout 高度设置为 match_parent:

<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:stretchColumns="*"> 

并确保根据您的方向删除代码。

【讨论】:

  • 列完美匹配问题在于行高。
猜你喜欢
  • 2016-07-30
  • 2022-01-15
  • 1970-01-01
  • 1970-01-01
  • 2020-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-21
相关资源
最近更新 更多