【问题标题】:Layout pixels and Canvas pixels different?布局像素和画布像素不同?
【发布时间】:2014-09-14 10:02:37
【问题描述】:

所以,我有 Framelayout 女巫有 SurfaceView(0) 和 xml 布局(1)。我的布局高度是 200px(下),我正在绘制 200px 高的矩形进行测试。出于某种奇怪的原因,布局似乎占用了更多的空间棕褐色矩形。无论屏幕大小或密度如何,我怎么能做到这一点,布局将是矩形? (我希望这不会太混乱......)

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="200px"
android:orientation="horizontal"
android:id="@+id/game_ui
"
style="@style/AppTheme">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="0.5"
    >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="0.2"
        android:text="Points"
        android:textSize="30sp"
        android:gravity="center"
        />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5"
        android:text="500"
        android:textSize="60sp"
        android:gravity="center"
        />

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="0.5"
    >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.2"
        android:text="Time"
        android:textSize="30sp"
        android:gravity="center"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5"
        android:text="500"
        android:textSize="60sp"
        android:gravity="center"/>


</LinearLayout>

画一个矩形:

Graphics g = game.getGraphics();
    g.clearScreen(Color.BLACK);   
    g.drawRect(0, 0, wildth, 200, Color.GREEN);
    //draws 200px high rectangle onto surfaceView

结果是这样的:

http://tinypic.com/r/sb6h55/8

【问题讨论】:

    标签: java android xml android-linearlayout surfaceview


    【解决方案1】:

    不要在布局或代码中使用硬编码像素。

    在布局中使用与密度无关的像素 (DP)。

    android:layout_height="200dp" 
    

    将代码中的像素转换为DP

    final int height = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, getResources().getDisplayMetrics());
    final int width = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, getResources().getDisplayMetrics());
    
    Graphics g = game.getGraphics();
    g.clearScreen(Color.BLACK);   
    g.drawRect(0, 0, width, height, Color.GREEN);
    

    欲了解更多信息,请阅读

    上的所有内容

    http://developer.android.com/guide/practices/screens_support.html#screen-independence

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多