2个框架/布局在1个活动中

时间:2014-11-24 15:09:19

标签: java android eclipse layout frames

我是android编程的新手。我在这里是因为我想要你的帮助。我正在为我的论文设计一个活动。

Heres the Image

我想要一个包含2个Frames / Layout的活动。上框有按钮,当我点击上框上的按钮时,下框会显示一些东西。

我会用什么来实现这一目标?

提前谢谢。

2 个答案:

答案 0 :(得分:2)

您可以使用LinearLayoutlayout_weight param将布局拆分为一半,如下所示:

<LinearLayout 
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

    <FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"></FrameLayout>

    <FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"></FrameLayout>

</LinearLayout>

layout_weight参数在决定谁获取空间时作为优先级,给定其他desciption将导致任何FrameLayouts填充整个父级。您可以在documentation找到更多信息。

答案 1 :(得分:1)

以下是完整的解决方案:

步骤1: 将main_layout.xml文件创建到布局文件夹中:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="bottom">

        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button1"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"/>
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button2"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"/>
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="New Text"
        android:id="@+id/textView" />
</LinearLayout>

第2步:创建MainActivity:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;


public class MainActivity extends Activity {

private View mBtn1;
private View mBtn2;
private TextView mTxt;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_layout);

    mBtn1 = findViewById(R.id.button1);
    mBtn2 = findViewById(R.id.button2);
    mTxt = (TextView)findViewById(R.id.textView);

    mBtn1.setOnClickListener(mBtnClickListener);
    mBtn2.setOnClickListener(mBtnClickListener);

}

View.OnClickListener mBtnClickListener = new View.OnClickListener() {

    /**
     * This is all sync because we want to have only one event at a time
     * @param v
     */
    @Override
    public synchronized void onClick(View v) {

        switch (v.getId()) {
            case R.id.button1:
                mTxt.setText("button1");
                break;
            case R.id.button2:
                mTxt.setText("button2");
                break;

        }
        }
    } ;
    }

第3步:将激活添加到AndroidManifest.xml中:

<activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>