Android的布局垂直/水平重量 [英] android layout vertical/horizontal weight

查看:113
本文介绍了Android的布局垂直/水平重量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用机器人的LinearLayout 为每个设备的不同分辨率。这样,我解决了垂直布局问题,不同的分辨率。

但我解决不了水平布局问题。 Ihis是我的布局。我想建的布局是这样的。

在这种情况下,我使用保证金左​​/右,但这不是在不同的分辨率下工作。对决议如何放置的TextView 关联?

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:ID =@ + ID / LinearLayout1
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:背景=@绘制/背景
机器人:方向=垂直><的TextView
    机器人:ID =@ + ID / titleview的
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:layout_weight =9
    机器人:比重=中心
    机器人:文字=TextView的
    机器人:文字颜色=#5a5856
    机器人:TEXTSIZE =35SP/><的TextView
    机器人:ID =@ + ID / wordDafinitionView
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:layout_weight =3
    机器人:TEXTSIZE =17sp
    机器人:layout_marginLeft =25dp
    机器人:layout_marginRight =30dp
    机器人:文字=TextView的
    机器人:文字颜色=#5a5856/>
<的TextView
    机器人:ID =@ + ID / TextView01
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:layout_weight =9
    机器人:比重=中心
    机器人:文字=TextView的
    机器人:文字颜色=#5a5856
    机器人:TEXTSIZE =35SP/>< / LinearLayout中>


解决方案

解决方案之一是你可以用

阅读屏幕尺寸

  DisplayMetrics指标= getResources()getDisplayMetrics()。
INT宽度= metrics.widthPixels;
INT高度= metrics.heightPixels;

和检查不同的设备的屏幕尺寸,

  IF((metrics.heightPixels / metrics.density)GT = 700)
                {
                    Log.e(10英寸的标签,10英寸选项卡);
                }
            否则,如果((metrics.heightPixels / metrics.density)GT = 550)
                {
                    Log.e(7寸标签,7英寸选项卡);
                }
            否则,如果((metrics.heightPixels / metrics.density)GT = 400)
                {
                    Log.e(5英寸选项卡/移动,5英寸选项卡/手机);
                }
            其他
                {
                    Log.e(所有其他移动设备,移动的所有其他设备);
                }

,然后相应地设置您的TextView的宽度。

I use Androids LinearLayout for each device's different resolution. This way I solve vertical layout problem different resolution.

But I can't solve horizontal layout problem. Ihis is my layout. I want build layout like this.

In this case I use margin left / right but this is not work on different resolution. How to place TextView relate on resolution?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical" >

<TextView
    android:id="@+id/titleView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="9"
    android:gravity="center"
    android:text="TextView"
    android:textColor="#5a5856"
    android:textSize="35sp" />

<TextView
    android:id="@+id/wordDafinitionView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="3"
    android:textSize="17sp"
    android:layout_marginLeft="25dp"
    android:layout_marginRight="30dp"
    android:text="TextView"
    android:textColor="#5a5856" />


<TextView
    android:id="@+id/TextView01"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="9"
    android:gravity="center"
    android:text="TextView"
    android:textColor="#5a5856"
    android:textSize="35sp" />

</LinearLayout>

解决方案

One solution is you can read the screen sizes with

DisplayMetrics metrics = getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;

and check different device screen sizes,

if((metrics.heightPixels/metrics.density) >= 700)
                {
                    Log.e(" 10 inch Tab", " 10 inch Tab");
                }
            else if((metrics.heightPixels/metrics.density) >= 550)
                {
                    Log.e(" 7 inch Tab", " 7 inch Tab");
                }
            else if((metrics.heightPixels/metrics.density) >= 400)
                {
                    Log.e(" 5 inch Tab / Mobile", " 5 inch Tab / Mobile");
                }
            else
                {
                    Log.e("All other mobile devices", "All mobile other devices");
                }

and then set the width of your TextView accordingly.

这篇关于Android的布局垂直/水平重量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆