0

我是 android 应用程序开发的新手。我正在尝试创建一个计算器应用程序,我有 2 个问题要问。

1. 如何在LinearLayout中新建一行?它目前看起来像这样:

[文本视图][文本视图][按钮]

我希望它看起来像这样:

[文本视图][文本视图]

[按钮]

这是我的 xml 代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/number1"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:hint="@string/number1" />

<TextView
    android:id="@+id/number2"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:hint="@string/number2" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_calculate" />

2.如何处理“number1”和“number2”Textviews的总和并将其显示在Main Activity(TextViews所在的位置)上?

4

3 回答 3

0

线性布局可以是水平或垂直的,如果您需要行和列,则需要使用相对布局,或者只需添加一个以上的线性布局,例如..

TextView txtvar = (TextView) findViewById(R.id.textViewIdFromXml);
txtvar.setText("blah");

这就是你如何连接到带有 id textViewIdFromXml 的 textview 元素并为文本设置 blah

于 2013-01-05T16:55:15.920 回答
0
  1. Nested linear layouts.

For example (pseudo xml):

<Linear Layout : Vertical>
   <Linear Layout : Horizontal>
       <TextView />
       <TextView />
   </Linear Layout>
   <Button />
</Linear Layout>

Edit: I tried the following in Eclipse and for me it does what I think you ask (though it doesn't look fancy):

<?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" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

    </LinearLayout>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>
于 2013-01-05T16:58:39.047 回答
0

第一个答案:创建一个垂直线性布局并使用嵌套在垂直布局中的水平布局。但我更喜欢相对布局。

2ns Ans:用于onClickListener()按钮。& 在每次onClickListener()更改数字显示文本框的文本下使用setText(). 但是在 = 按钮上onClickListener()使用数字显示文本框的值,getText()并将其用于 + - 或 * /

于 2013-01-05T17:32:34.063 回答