我试图建立一个国际象棋应用程序,但我不能让董事会正常工作。我已经尝试了一切使表格行高度与列宽相匹配,使得正方形看起来是方形的,但是在不同的屏幕上,它们会被挤压或拉伸。
这是我的XML代码:
<TableLayout
android:id="@+id/chessboard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/background" >
<!-- LINE 8 -->
<TableRow
android:id="@+id/line_8"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="30dp" >
<!-- NOTATION LINE 8 LEFT -->
<TextView
android:id="@+id/notation_8_left"
android:layout_width="35dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center|right"
android:paddingRight="3dp"
android:text="8"
android:textColor="#FEFEFE" />
<!-- CELLS -->
<ImageButton
android:id="@+id/a8"
android:background="#fe9"
android:onClick="moveClickHandler" android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="35dp"
android:scaleType="fitXY"
android:src="@drawable/brook"
android:tag="br" />
<ImageButton
android:id="@+id/b8"
android:layout_width="35dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#fb3"
android:onClick="moveClickHandler"
android:scaleType="fitXY"
android:src="@drawable/bknight"
android:tag="bn" />
<ImageButton
android:id="@+id/c8"
android:background="#fe9"
android:onClick="moveClickHandler" android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="35dp"
android:scaleType="fitXY"
android:src="@drawable/bbishop"
android:tag="bb" />
<ImageButton
android:id="@+id/d8"
android:background="#fb3"
android:onClick="moveClickHandler" android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="35dp"
android:scaleType="fitXY"
android:src="@drawable/bqueen"
android:tag="bq" />
...
</TableRow>
这是我尝试读取屏幕宽度并相应地设置行高的程序性尝试:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
squareWidth = size.x / 10;
LayoutParams params = new LayoutParams(TableRow.LayoutParams.MATCH_PARENT, squareWidth);
TableRow rank8 = (TableRow) findViewById(R.id.line_8);rank8.setLayoutParams(params);
TableRow rank7 = (TableRow) findViewById(R.id.line_7);rank7.setLayoutParams(params);
TableRow rank6 = (TableRow) findViewById(R.id.line_6);rank6.setLayoutParams(params);
TableRow rank5 = (TableRow) findViewById(R.id.line_5);rank5.setLayoutParams(params);
TableRow rank4 = (TableRow) findViewById(R.id.line_4);rank4.setLayoutParams(params);
TableRow rank3 = (TableRow) findViewById(R.id.line_3);rank3.setLayoutParams(params);
TableRow rank2 = (TableRow) findViewById(R.id.line_2);rank2.setLayoutParams(params);
TableRow rank1 = (TableRow) findViewById(R.id.line_1);rank1.setLayoutParams(params);
当我超出高度和&#34; squareWidth的值时,&#34;我得到&#34; squareWidth是160&#34;但是&#34;行高是90&#34;或&#34; squareWidth为35,行高为68&#34;。所以感觉就像Android正在弥补它想要的任何价值。如何强制行高度合适?!
答案 0 :(得分:0)
在您设置的XML中:
android:layout_height="fill_parent" android:layout_width="35dp"
为什么不:
android:layout_height="35dp" android:layout_width="35dp"
(另外,为什么要指定layout_weight?那不应该在那里。)