我正在学习Android编程。我正在尝试使用Android GridLayout开发计算器应用程序。我无法在计算器的数字键盘中显示最后一行。直到代码中的第四行,我能够正确地看到布局。添加第五行后,我无法看到整个数字键盘布局。这种布局有什么问题?
import
屏幕截图Till-Row 4
截图 - 第5行
答案 0 :(得分:0)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_below="@id/toolbarHome">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:textSize="38dp"
android:textColor="#FFF"
android:textAlignment="textEnd"
android:text="0"
android:background="#333"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="AC"/>
<Button
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="%"/>
<Button
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="/"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="7"/>
<Button
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="8"/>
<Button
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="9"/>
<Button
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="*"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="4"/>
<Button
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="5"/>
<Button
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="6"/>
<Button
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="-"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1"/>
<Button
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2"/>
<Button
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3"/>
<Button
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="+"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="."/>
<Button
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="0"/>
<Button
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="="/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
答案 1 :(得分:0)
这是网格布局:
<android.support.v7.widget.GridLayout
android:id="@+id/gLNumpad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/lLDisplay"
app:columnCount="4">
<Button
style="@style/CalcButtonStyle"
android:text="AC"
app:layout_columnWeight="1"
app:layout_columnSpan="2"/>
<Button
style="@style/CalcButtonStyle"
android:text="%"
app:layout_columnWeight="1"/>
<Button
style="@style/CalcButtonStyle"
android:text="/"
app:layout_columnWeight="1"/>
<Button
style="@style/CalcButtonStyle"
android:text="7"
app:layout_columnWeight="1"/>
<Button
app:layout_columnWeight="1"
style="@style/CalcButtonStyle"
android:text="8"/>
<Button
app:layout_columnWeight="1"
style="@style/CalcButtonStyle"
android:text="9"/>
<Button
app:layout_columnWeight="1"
style="@style/CalcButtonStyle"
android:text="x"/>
<Button
app:layout_columnWeight="1"
style="@style/CalcButtonStyle"
android:text="4"/>
<Button
app:layout_columnWeight="1"
style="@style/CalcButtonStyle"
android:text="5"/>
<Button
app:layout_columnWeight="1"
style="@style/CalcButtonStyle"
android:text="6"/>
<Button
app:layout_columnWeight="1"
style="@style/CalcButtonStyle"
android:text="-"/>
<Button
app:layout_columnWeight="1"
style="@style/CalcButtonStyle"
android:text="1"/>
<Button
app:layout_columnWeight="1"
style="@style/CalcButtonStyle"
android:text="2"/>
<Button
app:layout_columnWeight="1"
style="@style/CalcButtonStyle"
android:text="3"/>
<Button
app:layout_columnWeight="1"
style="@style/CalcButtonStyle"
android:text="+"/>
</android.support.v7.widget.GridLayout>
将其他重复按钮定义放在样式CalcButtonStyle
:
<style name="CalcButtonStyle" parent="Base.TextAppearance.AppCompat.Button">
<item name="android:textSize">40sp</item>
<item name="android:textStyle">bold</item>
<item name="android:padding">8dp</item>
</style>
答案 2 :(得分:0)
你的代码很好没有任何问题。
问题是属性layout_columnWeight
仅用于API级别21及更高版本。
我建议您使用 android.support.v7.widget.GridLayout 向后兼容并支持权重。
所以你只需要将build &#39; com.android.support:gridlayout-v7:26.1.0&#39; 添加到build.gradle。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="">
<LinearLayout
android:id="@+id/lLDisplay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#696969 "
android:text="0"
android:textAlignment="viewEnd"
android:textColor="#FFF8DC"
android:textSize="100sp" />
</LinearLayout>
<android.support.v7.widget.GridLayout
android:id="@+id/gLNumpad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/lLDisplay"
android:layout_gravity="center"
android:paddingTop="5dp"
app:alignmentMode="alignBounds"
app:columnCount="4"
app:useDefaultMargins="false"
app:rowCount="5">
<!-- Row 1-->
<Button
android:id="@+id/btnPower"
android:padding="8dp"
android:text="x"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textSize="40sp"
android:textStyle="bold"
app:layout_column="0"
app:layout_columnSpan="2"
app:layout_columnWeight="1"
app:layout_row="0"
app:layout_rowWeight="1" />
<Button
android:id="@+id/btnPercent"
android:padding="8dp"
android:text="%"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textSize="40sp"
android:textStyle="bold"
app:layout_column="2"
app:layout_row="0"
app:layout_columnWeight="1"
app:layout_rowWeight="1" />
<Button
android:id="@+id/btnDivision"
android:padding="8dp"
android:text="/"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textSize="40sp"
android:textStyle="bold"
app:layout_column="3"
app:layout_row="0"
app:layout_columnWeight="1"
app:layout_rowWeight="1" />
<!-- Row 2-->
<Button
android:id="@+id/btnSeven"
android:padding="8dp"
android:text="7"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textSize="40sp"
android:textStyle="bold"
app:layout_column="0"
app:layout_columnWeight="1"
app:layout_row="1"
app:layout_rowWeight="1" />
<Button
android:id="@+id/btnEight"
android:padding="8dp"
android:text="8"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textSize="40sp"
android:textStyle="bold"
app:layout_columnWeight="1"
app:layout_column="1"
app:layout_row="1"
app:layout_rowWeight="1" />
<Button
android:id="@+id/btnNine"
android:padding="8dp"
android:text="9"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textSize="40sp"
android:textStyle="bold"
app:layout_column="2"
app:layout_columnWeight="1"
app:layout_row="1"
app:layout_rowWeight="1" />
<Button
android:id="@+id/btnMultiply"
android:padding="8dp"
android:text="*"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textSize="40sp"
android:textStyle="bold"
app:layout_column="3"
app:layout_columnWeight="1"
app:layout_row="1"
app:layout_rowWeight="1" />
<!-- Row 3-->
<Button
android:id="@+id/btnFour"
android:padding="8dp"
android:text="4"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textSize="40sp"
android:textStyle="bold"
app:layout_column="0"
app:layout_columnWeight="1"
app:layout_row="2"
app:layout_rowWeight="1" />
<Button
android:id="@+id/btnFive"
android:padding="8dp"
android:text="5"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textSize="40sp"
android:textStyle="bold"
app:layout_column="1"
app:layout_columnWeight="1"
app:layout_row="2"
app:layout_rowWeight="1" />
<Button
android:id="@+id/btnSix"
android:padding="8dp"
android:text="6"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textSize="40sp"
android:textStyle="bold"
app:layout_column="2"
app:layout_row="2"
app:layout_columnWeight="1"
app:layout_rowWeight="1" />
<Button
android:id="@+id/btnSubtract"
android:padding="8dp"
android:text="-"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textSize="40sp"
android:textStyle="bold"
app:layout_column="3"
app:layout_columnWeight="1"
app:layout_row="2"
app:layout_rowWeight="1" />
<!-- Row 4-->
<Button
android:id="@+id/btnOne"
android:padding="8dp"
android:text="1"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textSize="40sp"
android:textStyle="bold"
app:layout_column="0"
app:layout_columnWeight="1"
app:layout_row="3"
app:layout_rowWeight="1" />
<Button
android:id="@+id/btnTwo"
android:padding="8dp"
android:text="2"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textSize="40sp"
android:textStyle="bold"
app:layout_column="1"
app:layout_columnWeight="1"
app:layout_row="3"
app:layout_rowWeight="1" />
<Button
android:id="@+id/btnThree"
android:padding="8dp"
android:text="3"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textSize="40sp"
android:textStyle="bold"
app:layout_column="2"
app:layout_columnWeight="1"
app:layout_row="3"
app:layout_rowWeight="1" />
<Button
android:id="@+id/btnAdd"
android:padding="8dp"
android:text="+"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textSize="40sp"
android:textStyle="bold"
app:layout_column="3"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
app:layout_row="3" />
<!-- Row 5-->
<Button
android:id="@+id/btnPlusMinus"
android:padding="8dp"
android:text="+/-"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textSize="40sp"
android:textStyle="bold"
app:layout_column="0"
app:layout_columnWeight="1"
app:layout_row="4"
app:layout_rowWeight="1" />
<Button
android:id="@+id/btnZero"
android:padding="8dp"
android:text="0"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textSize="40sp"
android:textStyle="bold"
app:layout_column="1"
app:layout_columnWeight="1"
app:layout_row="4"
app:layout_rowWeight="1" />
<Button
android:id="@+id/btnPeriod"
android:padding="8dp"
android:text="."
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textSize="40sp"
android:textStyle="bold"
app:layout_column="2"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
app:layout_row="4" />
<Button
android:id="@+id/btnEquals"
android:padding="8dp"
android:text="="
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textSize="40sp"
android:textStyle="bold"
app:layout_columnWeight="1"
app:layout_column="3"
app:layout_row="4"
app:layout_rowWeight="1" />
</android.support.v7.widget.GridLayout>