自定义视图重叠

问题描述 投票:0回答:1

我定义了我膨胀的布局行,并将实际视图添加到线性布局中。

我想要这样的东西,

https://i.stack.imgur.com/EKPmJ.png

这是我的主要活动的xml,我正在给服装视图行充气

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.skw.customeviewdemo.MainActivity">


<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/inflate"
    android:text="inflate"/>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/list_container">

    </LinearLayout>
</LinearLayout>

这是我正在膨胀的行的xml代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal">

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="its text view"
        />

    <Button
        android:id="@+id/nameButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/name"
        />
    <ImageView
        android:layout_marginTop="-25dp"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher_round"/>
</RelativeLayout>

这是我的java代码

public class MainActivity extends AppCompatActivity {

    Button b;
    LinearLayout l1;
    Context context;
    Boolean isViewCreated = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final String Namearray[] = {"sagar", "ranjeet", "akash", "kate"};
        this.context = this;

        b = (Button) findViewById(R.id.inflate);
        l1 = (LinearLayout) findViewById(R.id.list_container);

        createViews(Namearray);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(l1.getVisibility() == View.VISIBLE && isViewCreated)
                {
                    l1.setVisibility(View.GONE);
                }
                else
                {
                    l1.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    void createViews(final String[] namearray)
    {
        for(int i=0;i < namearray.length;i++){
            final int j = i;
            View view = LayoutInflater.from(context).inflate(R.layout.layout_item,null);
            TextView button1 = (TextView) view.findViewById(R.id.name);
            Button button2 = (Button) view.findViewById(R.id.nameButton);


            button1.setText("HELLO " + namearray[i]);
            button2.setText("Click to know my name ");

            button2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context,"Hello " + namearray[j],Toast.LENGTH_LONG).show();
                }
            });
            view.setId(generateViewId());

            try {
                l1.addView(view);
                isViewCreated = true;
            }
            catch (Exception e)
            {

            }
        }
        l1.setVisibility(View.GONE);
    }

    private static final AtomicInteger viewIdGenerator = new AtomicInteger(15000000);

    public static int generateViewId() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
            return generateUniqueViewId();
        } else {
            return View.generateViewId();
        }
    }

    private static int generateUniqueViewId() {
        while (true) {
            final int result = viewIdGenerator.get();
            // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
            int newValue = result + 1;
            if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
            if (viewIdGenerator.compareAndSet(result, newValue)) {
                return result;
            }
        }
    }
}

但当我膨胀行我的图像视图得到截止,我不想要我尝试负边距,但它在这里不起作用它看起来像custom overlapping row

我做了什么,所以我将图像叠加到上方视图上?

android relativelayout
1个回答
0
投票

尝试将布局更改为此。

<?xml version="1.0" encoding="utf-8"?>

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



    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="its text view"
            />

        <Button
            android:id="@+id/nameButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/name"
            />

    </RelativeLayout>

    <ImageView
        android:layout_marginTop="25dp"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher_round"/>


</RelativeLayout>

当你放入另一行时,需要将该行向上推,以便现在存在的android图标覆盖它。

© www.soinside.com 2019 - 2024. All rights reserved.