在相对布局中对齐自定义视图动态不工作android

时间:2015-10-08 09:33:06

标签: android android-relativelayout

我在相对布局中创建了一个自定义视图。

  

我可以通过xml在中心/左/右对齐,但不能   编程

自定义视图

public class HotspotView extends RelativeLayout {

    public HotspotView(Context context) {
        super(context);
        init(context);
    }

    public HotspotView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public HotspotView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.hotspot_view, this);
    }
}

自定义视图xml

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

    <ImageView
        android:id="@+id/hotspot_imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/tohome"

        />


</RelativeLayout>

活动xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black"

    >


    <VideoView
        android:id="@+id/vv_frame"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerInParent="true"
        android:clickable="true"
        android:onClick="onclick"
        android:visibility="gone" />

    <ImageView
        android:id="@+id/iv_frame"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:adjustViewBounds="true"
        android:contentDescription="@string/content_description"
        android:onClick="onclick"
        android:scaleType="fitXY"
        android:visibility="gone"

        />

    <ImageView
        android:id="@+id/lv_frame"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerInParent="true"
        android:contentDescription="@string/content_description"
        android:onClick="onclick"
        android:visibility="gone" />

    <


    <android.gesture.GestureOverlayView
        android:id="@+id/gesture_area"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1.0"
        android:alpha="1.0"
        android:background="@android:color/transparent"
        android:gestureColor="@android:color/white"
        android:onClick="onclick"
        android:visibility="visible" />







    <WebView
        android:id="@+id/ticker_webview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:visibility="gone"></WebView>

    <WebView
        android:id="@+id/wv_frame"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerInParent="true"
        android:adjustViewBounds="true"
        android:onClick="onclick"
        android:scaleType="fitXY"
        android:visibility="gone"

        />

    <ProgressBar
        android:id="@+id/webview_progressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_gravity="center"
        android:visibility="gone" />




        <com.devstring.imageframe.views.HotspotView
            android:id="@+id/hotspot"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:clickable="true"
            android:visibility="visible" />

</RelativeLayout>

代码:

 RelativeLayout.LayoutParams lp = new 

RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.MATCH_PARENT,
                        RelativeLayout.LayoutParams.MATCH_PARENT);
        lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        ImageFrameActivity.hotspotView.setLayoutParams(lp);
           ImageFrameActivity.hotspotView.setVisibility(View.VISIBLE);

我遇到了各种类似的问题,但我无法解决问题。

2 个答案:

答案 0 :(得分:1)

试试这个:

 RelativeLayout.LayoutParams lp = 
(RelativeLayout.LayoutParams) ImageFrameActivity.hotspotView.getLayoutParams();  
 lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
 ImageFrameActivity.hotspotView.setLayoutParams(lp);

答案 1 :(得分:0)

尝试使用  lp.gravity = Gravity.LEFT;