【问题标题】:2 Frames/layout in 1 Activity1 个活动中的 2 帧/布局
【发布时间】:2015-01-22 08:23:24
【问题描述】:

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

Heres the Image

我想要一个有 2 帧/布局的活动。上框有按钮,点击上框的按钮,下框会显示一些东西。

我会用什么来做到这一点?

提前谢谢你。

【问题讨论】:

  • 你应该先试试。那么,如果你有问题。发布您的代码以获得帮助。
  • @hasan83 我不知道如何开始。
  • 我认为你应该阅读一些java和android开发的基础知识......
  • @Opiatefuchs 是的,你是对的。谢谢.. :)

标签: java android eclipse layout frames


【解决方案1】:

您可以使用LinearLayoutlayout_weight 参数将布局分成两半,如下所示:

<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 参数在决定谁获得空间时作为优先级起作用,因为其他描述会导致任何 FrameLayouts 填充整个父级。您可以在documentation 中找到更多信息。

【讨论】:

  • 第二个FrameLayout可以用scrollView吗?
  • 如果外面没有ScrollView-是的。
  • @Opiatefuchs 我编辑了答案以增加一些清晰度 + 文档链接。
  • 非常感谢@atok 的大力帮助。
  • 如果这解决了问题,你能接受答案吗?不仅我会得到我宝贵的分数( :D )而且问题也不会显示为未回答。
【解决方案2】:

这里是完整的解决方案:

步骤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>

第二步:创建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;

        }
        }
    } ;
    }

第三步:将活动添加到您的 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>

【讨论】:

  • 天哪,谢谢先生.. 大帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-06
  • 2015-05-06
  • 1970-01-01
  • 1970-01-01
  • 2014-02-15
相关资源
最近更新 更多