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

Joh*_*ohn 9 android

我找到了一个简单的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);  

}
Run Code Online (Sandbox Code Playgroud)

Sur*_*gch 7

如果您想要以编程方式创建复杂的布局,那么在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>
Run Code Online (Sandbox Code Playgroud)

为您的视图创建一个容器

有一些地方可以将您的视图放在您的活动布局中.你可以有这样的东西.

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

</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

膨胀视图

使用获取对容器的引用,从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);

}
Run Code Online (Sandbox Code Playgroud)

这样做可以使你的代码更清洁.

也可以看看:


ale*_*ull 3

膨胀 R.layout.yellow 和 R.layout.red 的方式确实是正确的方法。您可以通过将大部分代码移至 xml 来简化代码。我认为 tv1 只是一个样本?如果没有,它可以进入 main.xml。您甚至可能找到一种通过单次膨胀来创建黄色和红色的方法……具体取决于您在做什么。

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