Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在相对布局中动态对齐自定义视图不工作_Android_Android Relativelayout - Fatal编程技术网

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

Android 在相对布局中动态对齐自定义视图不工作,android,android-relativelayout,Android,Android Relativelayout,我在相对布局中创建了一个自定义视图 我可以通过xml将它对齐到中间/左边/右边,但不能 程序化 自定义视图 public class HotspotView extends RelativeLayout { public HotspotView(Context context) { super(context); init(context); } public HotspotView(Context context, Attribut

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

我可以通过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>
<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>
我遇到过各种类似的问题,但我无法解决问题。

尝试使用
lp.gravity=gravity.LEFT

尝试以下操作:

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