0

私の主なレイアウトは次のようなものです:

~~~~~~~~~~
~~~~~~~~~~ 
---------- 
[][][][][]

ボタンがクリックされるたびに下側が固定され、上側が変化します。上部と下部に 2 つの異なる線形レイアウトを使用Activityし、下部のボタンごとに異なるレイアウトを使用します。実際には機能していますが、正しい方法かどうかはわかりません。使いすぎた気がするActivities

Activitiesこの種のセットアップを行ったり来たりする必要がありますか? または、この問題を解決するためのより良い方法はありますか?

4

1 に答える 1

0

答えは Singularity によって与えられます: フラグメント
http://developer.android.com/guide/components/fragments.html

フラグメントは、アクティビティの動作またはユーザー インターフェイスの一部を表します。複数のフラグメントを 1 つのアクティビティに結合して、マルチペイン UI を構築し、フラグメントを複数のアクティビティで再利用できます。フラグメントは、独自のライフサイクルを持ち、独自の入力イベントを受け取り、アクティビティの実行中に追加または削除できる、アクティビティのモジュラー セクションと考えることができます (「サブ アクティビティ」のようなもので、さまざまなアクティビティで再利用します)。

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.ViewGroup;

public class ArticleFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
     // Inflate the layout for this fragment
        return inflater.inflate(R.layout.article_view, container, false);
    }
}

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

<fragment android:name="com.example.android.fragments.HeadlinesFragment"
          android:id="@+id/headlines_fragment"
          android:layout_weight="1"
          android:layout_width="0dp"
          android:layout_height="match_parent" />

<fragment android:name="com.example.android.fragments.ArticleFragment"
          android:id="@+id/article_fragment"
          android:layout_weight="2"
          android:layout_width="0dp"
          android:layout_height="match_parent" />

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news_articles);
    }
}
于 2013-01-17T15:52:22.313 に答える