2012-11-15 31 views
0

我有一个视图分类创建自定义control.Below是我在其中我试图设置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> 

我想将视图定位在屏幕的底部。

请帮我一把。

回答

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等
也有可能relativeley把他们彼此。

+0

我改成了相对布局,但hegiht不reduced.please让我知道 – user198725878

+0

哪个高度?您将其设置为setMinimumHeight(100);所以视图的高度总是> = 100像素。 – Thommy

2

您应该在您的自定义视图上使用RelativeLayout并使用android:layout_alignParentBottom="true"。你可以直接把你的类名作为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>