定位视图

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

我已将View细分为创建自定义控件的视图。下面是我尝试设置x,y,宽度和高度的代码。

但是我无法设置x和y来将视图放置在屏幕上。

    public class MyAdvertisement extends View{

        public MyAdvertisement(){
            super(null);
            System.out.println("This first");
            this.setBackgroundColor(android.graphics.Color.RED);
            this.setMinimumWidth(500);
            this.setMinimumHeight(100);
        }

        public MyAdvertisement(Context context){
            super(context);
            this.setBackgroundColor(android.graphics.Color.YELLOW);
            this.setMinimumWidth(500);
            this.setMinimumHeight(100);
        }

        public MyAdvertisement(Context context, AttributeSet attrs){
            super(context, attrs);
            this.setBackgroundColor(android.graphics.Color.YELLOW);
            this.setMinimumWidth(500);
            this.setMinimumHeight(50);
            LayoutParams layoutParams=new LayoutParams(500,50);
            layoutParams.setMargins(0, 400,0,0);
            this.setLayoutParams(new LinearLayout.LayoutParams(layoutParams));

        }

        public MyAdvertisement(Context context, AttributeSet attrs, int defStyle){
            super(context, attrs, defStyle);
            this.setBackgroundColor(android.graphics.Color.GREEN);
            this.setMinimumWidth(500);
            this.setMinimumHeight(100);

        }

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
    <view
        class="com.rciicustomclass.MyAdvertisement"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/myb"
        android:tag="tag"
         />
</LinearLayout>

我想将视图放置在屏幕底部。

android android-layout subclass
2个回答
2
投票

我建议使用RelativeLayout:

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

    <"com.rciicustomclass.MyAdvertisement"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" />

</RelativeLayout>

此外,还有许多其他标签可以将视图定位,例如居中,alignParentLeft,centerVertical,centerHorizo​​ntal等。也可以将它们相对地放置。


2
投票

您应该在自定义视图上使用带有android:layout_alignParentBottom="true"的RelativeLayout。您可以直接将类名称作为XML元素的名称:

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <com.rciicustomclass.MyAdvertisement
        android:id="@+id/myb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:tag="tag" />

</RelativeLayout>
© www.soinside.com 2019 - 2024. All rights reserved.