简体   繁体   English

如何在等距的线性布局中添加子布局

[英]How to add child layout in linear layout which will be equally spaced

I have a linear layout which has horizontal orientation. 我有一个水平方向的线性布局。 What i want to do is, i want to add images at runtime and those images should be equally spaced in the linear layout. 我想做的是,我想在运行时添加图像,并且这些图像应在线性布局中均匀间隔。 For better understanding here is the desired output. 为了更好地理解,这里是所需的输出。

在此处输入图片说明

But i am getting both images at the right end one after another. 但是我在正确的一端接连得到两个图像。

Here is the code snippets. 这是代码片段。

1) Parent layout (test.xml) 1)父级布局(test.xml)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/test"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></LinearLayout>`

2)Child Layout (test_image.xml) 2)子布局(test_image.xml)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/testTab"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
>
<ImageView
    android:id="@+id/testImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />
 </LinearLayout>

Here is the Activity where i am adding children in parent layout` 这是我在父级布局中添加子级的活动。

public class Test extends AppCompatActivity {

private  LayoutInflater inflater;

@Override
protected void onCreate(Bundle savedInstanceStste)
{
    super.onCreate(savedInstanceStste);
    setContentView(R.layout.test);

    inflater = (LayoutInflater) Test.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout layout = (LinearLayout)findViewById(R.id.test);

    LinearLayout tab = provideTab(R.drawable.rottentomatoes);
    LinearLayout tab1 = provideTab(R.drawable.rottentomatoes);

    layout.addView(tab);
    layout.addView(tab1);

}

public LinearLayout provideTab(int id)
{

    LinearLayout cardView = (LinearLayout)inflater.inflate(R.layout.test_image,null);

    ImageView imageView = (ImageView)cardView.findViewById(R.id.testImage);
    imageView.setImageResource(id);

    return cardView;
}}

What am i missing here. 我在这里想念什么。
(When i add child layout in parent layout using include tag images are getting equally spaced). (当我使用包含标签图像在父级布局中添加子级布局时,它们之间的间距相等)。
Thanks in advance 提前致谢

Change the first line on your provideTab method to 将您的provideTab方法的第一行更改为

LinearLayout cardView = (LinearLayout)inflater.inflate(R.layout.test_image, layout, false);

where layout is the parent LinearLayout (returned from (LinearLayout)findViewById(R.id.test) ). 其中layout是父级LinearLayout(从(LinearLayout)findViewById(R.id.test) )。

That second param will provide a set of LayoutParams values for root of the returned hierarchy. 第二个参数将为返回的层次结构的根提供一组LayoutParams值。 Read more here . 在这里阅读更多。

Your child views need to know where they are attaching to get the weights right. 您的孩子视图需要知道它们连接的位置,以使权重正确。 That is why including works but your code doesn't. 这就是为什么包括有效但您的代码无效的原因。 Try the following code: 尝试以下代码:

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;

import junit.framework.Test;

public class MainActivity extends AppCompatActivity {
    private LayoutInflater inflater;
    LinearLayout layout;

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

        inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        layout = (LinearLayout) findViewById(R.id.test);

        LinearLayout tab = provideTab(R.mipmap.ic_launcher_round);
        LinearLayout tab1 = provideTab(R.mipmap.ic_launcher_round);

        layout.addView(tab);
        layout.addView(tab1);
    }

    public LinearLayout provideTab(int id) {

        // Let the inflater know what the parent ViewGroup is (layout) but don't attach (false)
        LinearLayout cardView = (LinearLayout) inflater.inflate(R.layout.test_image, layout, false);

        ImageView imageView = (ImageView) cardView.findViewById(R.id.testImage);
        imageView.setImageResource(id);

        return cardView;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM