所以,我有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
结果如下:
答案 0 :(得分:0)
请勿在布局或代码中使用硬编码像素。
在布局中使用与密度无关的像素(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