想通过此布局实现控件的平均分布,预期效果图如下:
代码如下:
最关键的部分是空间中的如下两行
android:layout_rowWeight="1" android:layout_columnWeight="1"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="300dp" />
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="4"
android:rowCount="5">
<Button
android:id="@+id/keyCE"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="CE"
/>
<Button
android:id="@+id/keyChu"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="/"/>
<Button
android:id="@+id/keyCheng"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="X"/>
<Button
android:id="@+id/keyC"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="C"/>
<Button
android:id="@+id/key7"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="7"/>
<Button
android:id="@+id/key8"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="8"/>
<Button
android:id="@+id/key9"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="9"/>
<Button
android:id="@+id/keyJia"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="+"/>
<Button
android:id="@+id/key4"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="4"/>
<Button
android:id="@+id/key5"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="5"/>
<Button
android:id="@+id/key6"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="6"/>
<Button
android:id="@+id/keyjian"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="-"/>
<Button
android:id="@+id/key1"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="1"/>
<Button
android:id="@+id/key2"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="2"/>
<Button
android:id="@+id/key3"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="3"/>
<Button
android:id="@+id/keyKaiPingFang"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="sqrt(2)"/>
<Button
android:id="@+id/keyQuDaoShu"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="1/X"/>
<Button
android:id="@+id/key0"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="0"/>
<Button
android:id="@+id/keyPiont"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="."/>
<Button
android:id="@+id/keyDeng"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="="/>
</GridLayout>
</LinearLayout>