【问题标题】:Create new FrameLayout on button click - Android在按钮单击时创建新的 FrameLayout - Android
【发布时间】:2013-12-17 20:43:10
【问题描述】:

每次单击按钮时,我都会尝试创建一个新的 FrameLayout。目前我已经手动实现了 3 个 FrameLayouts,当我单击按钮时,所有 3 个都设置为可见。我现在想要的只是将一个 FrameLayout 设置为可见,然后如果再次单击该按钮,则会出现第二个 FrameLayout 等等。java 代码:

public class MainActivity extends Activity {

    public final static String EXTRA_MESSAGE = "com.example.viginti.MESSAGE";
    int hoursValue;
    int minutesValue;
    int finalMinutes;
    int finalHours;
    int correctDivision;
    int timeLeft;
    int twentyFour;
    int i, x;
    NumberPicker np_hours;
    NumberPicker np_minutes;

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

        fillArray();
    }

    @Override
    public boolean onCreateOptionsMenu( Menu menu ) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate( R.menu.main, menu );
        return true;
    }

    public void generateAnswer( View view ) {

        hoursValue = np_hours.getValue();
        minutesValue = np_minutes.getValue();

        try {
            twentyFour = 24;
            correctDivision = 10;
            finalMinutes = minutesValue / correctDivision;
            finalHours = twentyFour - hoursValue;
            timeLeft = finalHours - finalMinutes;
        } catch( NumberFormatException e ) {
            /** DEBUGGING */
            System.out.println( "Number Format Exception: " + e );
        }
        String finalResult = Integer.toString( timeLeft );
        Intent displayData = new Intent( this, DisplayData.class );
        displayData.putExtra( EXTRA_MESSAGE, finalResult );
        startActivity( displayData );
    }

    public void fillArray() {
        np_hours = ( NumberPicker ) findViewById( R.id.hourNumber );
        np_minutes = ( NumberPicker ) findViewById( R.id.minuteNumber );
        String[] hoursArray = new String[25];
        String[] minutesArray = new String[61];
        for( i = 0; i < hoursArray.length; i++ ) {
            hoursArray[i] = Integer.toString( i );
        }

        for( x = 0 ; x < minutesArray.length; x++ ){
            minutesArray[x] = Integer.toString( x );
        }
        np_hours.setMinValue( 0 );
        np_hours.setMaxValue( 24 );
        np_hours.setWrapSelectorWheel( false );
        np_hours.setDisplayedValues( hoursArray );

        np_minutes.setMinValue( 0 );
        np_minutes.setMaxValue( 60 );
        np_minutes.setWrapSelectorWheel( false );
        np_minutes.setDisplayedValues( minutesArray );
    }

    @SuppressLint("NewApi")
    public void adActivity( View view ) {

        //Add first frame
        FrameLayout addActivities = ( FrameLayout )findViewById( R.id.frameLayout2 );
        addActivities.setVisibility( View.VISIBLE );

        //Add second frame
        FrameLayout addActivities1 = ( FrameLayout )findViewById( R.id.frameLayout3 );
        addActivities1.setVisibility( View.VISIBLE );
        //Add third frame
        FrameLayout addActivities2 = ( FrameLayout )findViewById( R.id.frameLayout4 );
        addActivities2.setVisibility( View.VISIBLE );
        fillArray();
    }
}

XML:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >


    <ImageView
        android:id="@+id/featuredimage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="18dp"
        android:clickable="true"
        android:onClick="generateAnswer"
        android:scaleType="centerCrop"
        android:src="@drawable/generate_button" />

      <ImageView
        android:id="@+id/addActivity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="50dp"
        android:clickable="true"
        android:onClick="adActivity"
        android:src="@drawable/add_activity" />

    <FrameLayout
        android:id="@+id/frameLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal" >

            <Spinner
                android:id="@+id/plan"
                android:layout_width="121dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="0dp"
                android:layout_marginTop="15dp"
                android:entries="@array/daily_plans" />

            <NumberPicker
                android:id="@+id/hourNumber"
                android:layout_width="40dp"
                android:layout_height="70dp"
                android:layout_marginLeft="24dp"/>

            <NumberPicker
                android:id="@+id/minuteNumber"
                android:layout_width="40dp"
                android:layout_height="70dp"
                android:layout_marginLeft="10dp" />
        </LinearLayout>
    </FrameLayout>

    <FrameLayout
        android:id="@+id/frameLayout2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/frameLayout1"
        android:visibility="gone">


        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal" >

            <Spinner
                android:id="@+id/plan1"
                android:layout_width="121dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="0dp"
                android:layout_marginTop="15dp"
                android:entries="@array/daily_plans" />

            <NumberPicker
                android:id="@+id/hourNumber"
                android:layout_width="40dp"
                android:layout_height="70dp"
                android:layout_marginLeft="24dp"/>

            <NumberPicker
                android:id="@+id/minuteNumber"
                android:layout_width="40dp"
                android:layout_height="70dp"
                android:layout_marginLeft="10dp" />
        </LinearLayout>
    </FrameLayout>
    <FrameLayout
        android:id="@+id/frameLayout3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/frameLayout2"
        android:visibility="gone">


        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal" >

            <Spinner
                android:id="@+id/plan1"
                android:layout_width="121dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="0dp"
                android:layout_marginTop="15dp"
                android:entries="@array/daily_plans" />

            <NumberPicker
                android:id="@+id/hourNumber"
                android:layout_width="40dp"
                android:layout_height="70dp"
                android:layout_marginLeft="24dp"/>

            <NumberPicker
                android:id="@+id/minuteNumber"
                android:layout_width="40dp"
                android:layout_height="70dp"
                android:layout_marginLeft="10dp" />
        </LinearLayout>
    </FrameLayout>
    <FrameLayout
            android:id="@+id/frameLayout4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/frameLayout3"
            android:visibility="gone">


            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="horizontal" >

                <Spinner
                    android:id="@+id/plan1"
                    android:layout_width="121dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="0dp"
                    android:layout_marginTop="15dp"
                    android:entries="@array/daily_plans" />

                <NumberPicker
                    android:id="@+id/hourNumber"
                    android:layout_width="40dp"
                    android:layout_height="70dp"
                    android:layout_marginLeft="24dp"/>

                <NumberPicker
                    android:id="@+id/minuteNumber"
                    android:layout_width="40dp"
                    android:layout_height="70dp"
                    android:layout_marginLeft="10dp" />
            </LinearLayout>
        </FrameLayout>

</RelativeLayout>

值得一提的是,我对 Android 开发还很陌生。另外,如果有更好的方法在每次单击按钮时生成 FrameLayouts(并正确定位它们),我很想听听!

非常感谢您的帮助!

【问题讨论】:

  • 可能有更好的方法来创建您正在制作的效果。为什么要将这些添加到布局中?

标签: java android xml android-framelayout


【解决方案1】:

您的解决方案没有任何问题,只要它按照您的意愿行事。事实上,我喜欢你的解决方案而不是以编程方式添加小部件,因为,

  • 您可以将它们放置在您想要的所有位置,并使其 在可视化编辑器中看起来不错;
  • 您可以使用不同的资源(如样式、尺寸、布局和可绘制对象)使其看起来更美观 不同的屏幕;
  • 您可以为横向和平板电脑创建不同的布局;
  • 同时,您可以轻松地在代码中连接小部件(因为您已经在 XML 文件中为它们提供了一个 ID)。

您可能希望将其全部放在滚动视图中,以应对无法使其适合小屏幕的情况。

【讨论】:

  • 非常感谢!是的,我发现这更容易,我只需要在单击按钮时添加一个布局(而不是所有 3 个,即每次单击按钮时 1 个)。
猜你喜欢
  • 1970-01-01
  • 2012-09-06
  • 1970-01-01
  • 1970-01-01
  • 2013-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多