以编程方式扩展视图的最佳方法

时间:2011-10-28 01:44:47

标签: android

我发现了一个简单的SwipeSample,我更改了这个SwipeSample以允许我创建新的xml布局并使主布局膨胀以显示它们。我想要做的还是能够以编程方式为滑动过程添加布局。

我有main.xml布局和red.xml和yellow.xml,这是一个简单的linearlayout,textview设置为纯色。

下面的代码有效,但我不认为这是正确的,也不是我想要做的最佳方式。 如果有人能提出一个更好的方式,将非常感激。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //Create a layout with a solid blue background programmatically
    TextView tv1 = new TextView(this);
    tv1.setText("Blue");
    tv1.setBackgroundColor(Color.BLUE);
    tv1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);
    ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    ll.addView(tv1);
    //Create a layout with a solid green background programmatically
    TextView tv2 = new TextView(this);
    tv2.setText("Green");
    tv2.setBackgroundColor(Color.GREEN);
    tv2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

    LinearLayout ll2 = new LinearLayout(this);
    ll2.setOrientation(LinearLayout.VERTICAL);
    ll2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    ll2.addView(tv2);
    //inflate the flipper view and add the yellow and red xml layouts and also the 2 programmatically created layouts
    fSpace = (ViewFlipper)findViewById(R.id.flipper);
    inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.yellow, fSpace);
    inflater.inflate(R.layout.red, fSpace);
    fSpace.addView(ll);
    fSpace.addView(ll2);  

}

2 个答案:

答案 0 :(得分:8)

如果您想要以编程方式创建复杂的布局,最简单的方法是在xml中预先制作布局,然后只需对其进行充气并在运行时添加它。

在xml中创建视图

以下是布局文件夹中的预制xml布局示例。你的可以是任何东西,单一视图或整个复杂的布局。

布局/ my_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/TextView1"
        android:text="This is a TV"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/TextView2"
        android:text="How are you today?"/>
</LinearLayout>

为您的视图制作容器

有一些地方可以将您的观点放在您的活动布局中。你可以有这样的东西。

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

</FrameLayout>

给视图充气

使用获取对容器的引用,从xml中提升视图,然后将其添加到容器中。

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

    FrameLayout container = (FrameLayout) findViewById(R.id.flContainer);
    View inflatedLayout= getLayoutInflater().inflate(R.layout.my_view, null, false);
    container.addView(inflatedLayout);

}

这样做会让您的代码更加清晰。

另见:

答案 1 :(得分:3)

你夸大R.layout.yellow和R.layout.red的方式确实是这样做的正确方法。您可以通过将大量代码移动到xml来简化代码。我认为tv1只是一个样本?如果没有,它可以进入main.xml。你甚至可以找到一种通过一次充气创造黄色和红色的方法......取决于你正在做什么。

在大多数情况下,以编程方式创建视图有点单调乏味。