Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.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-在RelativeLayout中,ImageView未显示在特定区域中_Android_Android Imageview_Android Relativelayout - Fatal编程技术网

Android-在RelativeLayout中,ImageView未显示在特定区域中

Android-在RelativeLayout中,ImageView未显示在特定区域中,android,android-imageview,android-relativelayout,Android,Android Imageview,Android Relativelayout,我不熟悉安卓系统。 我正在尝试做一个简单的应用程序。 有两个按钮(左和右)和一个图像。 图像根据单击的按钮向左或向右移动 问题如下图所示: 向左移动是可以的,但是当图像向右移动时,图像将消失在某些东西后面。请让我知道图像消失的原因以及如何解决此问题 这是activity_main.xml <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:andr

我不熟悉安卓系统。 我正在尝试做一个简单的应用程序。 有两个按钮(左和右)和一个图像。 图像根据单击的按钮向左或向右移动

问题如下图所示:

向左移动是可以的,但是当图像向右移动时,图像将消失在某些东西后面。请让我知道图像消失的原因以及如何解决此问题

这是activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
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"
tools:context="com.alticast.avoidpoo.MainActivity">

<RelativeLayout
    android:id="@+id/relative_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:layout_constraintBottom_creator="1"
    tools:layout_constraintLeft_creator="1"
    tools:layout_constraintRight_creator="1"
    tools:layout_constraintTop_creator="1">

    <ImageView
        android:id="@+id/img_android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/buttonLinearLayout"
        android:layout_centerHorizontal="true"
        android:visibility="visible"
        app:srcCompat="@mipmap/ic_launcher" />

    <LinearLayout
        android:id="@+id/buttonLinearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp">

        <Button
            android:id="@+id/btn_left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/left"
            android:visibility="visible" />

        <Button
            android:id="@+id/btn_right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/right"
            android:visibility="visible" />
    </LinearLayout>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>

这是MainActivity.class

package com.alticast.avoidpoo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity {

    int layout_width;          // the width of relative layout
    int layout_height;         // the height of relative layout

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    public void init() {
        Button btn_left = (Button) findViewById(R.id.btn_left);
        Button btn_right = (Button) findViewById(R.id.btn_right);
        btn_left.setOnClickListener(moveBtnClickListener);
        btn_right.setOnClickListener(moveBtnClickListener);

        final RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relative_layout);
        ViewTreeObserver vto = relativeLayout.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new     ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                layout_height = relativeLayout.getMeasuredHeight();
                layout_width = relativeLayout.getMeasuredWidth();
            }
        });
    }

    View.OnClickListener moveBtnClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View view) {

            ImageView img_android = (ImageView) findViewById(R.id.img_android);
            int id = view.getId();
            if (id == R.id.btn_left) {
                int x = img_android.getLeft();
                if (x-10 <= 0) return;
                img_android.setLeft(x-10);
            } else if (id == R.id.btn_right) {
                int x = img_android.getLeft();
                if (x+10 > layout_width-img_android.getWidth()) return;
                img_android.setLeft(x+10);
            }
        }
    };
}
package com.alticast.avoidpoo;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.view.ViewTreeObserver;
导入android.widget.Button;
导入android.widget.ImageView;
导入android.widget.RelativeLayout;
公共类MainActivity扩展了AppCompatActivity{
int layout_width;//相对布局的宽度
int layout_height;//相对布局的高度
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
公共void init(){
按钮btn_左=(按钮)findViewById(R.id.btn_左);
按钮btn_right=(按钮)findViewById(R.id.btn_right);
btn_left.setOnClickListener(movebtnclickllistener);
btn_right.setOnClickListener(movebtnclickllistener);
最终RelativeLayout RelativeLayout=(RelativeLayout)findViewById(R.id.relative_布局);
ViewTreeObserver vto=relativeLayout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(新的ViewTreeObserver.OnGlobalLayoutListener(){
@凌驾
公共图书馆{
布局高度=相对长度。getMeasuredHeight();
布局宽度=relativeLayout.getMeasuredWidth();
}
});
}
View.OnClickListener moveBtnClickListener=new View.OnClickListener(){
@凌驾
公共void onClick(视图){
ImageView img_android=(ImageView)findViewById(R.id.img_android);
int id=view.getId();
if(id==R.id.btn_左){
int x=img_android.getLeft();
if(x-10 layout_width-img_android.getWidth())返回;
img_android.setLeft(x+10);
}
}
};
}
============================================================================== 我使用setLayoutParams()方法解决了这个问题。 猜测直接在图像视图上调用setLeft()似乎不是一个好主意。这似乎是使用LayoutParams的关键

新的MainActivity.class如下所示:

View.OnClickListener moveBtnClickListener = new View.OnClickListener() {

    @Override
    public void onClick(View view) {
        ImageView img_android = (ImageView) findViewById(R.id.img_android);
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)img_android.getLayoutParams();

        int id = view.getId();
        if (id == R.id.btn_left) {
            if (params.leftMargin-10 < 0) return;
            params.leftMargin = params.leftMargin - 10;
        } else if (id == R.id.btn_right) {
            if (params.leftMargin+10 > layout_width-img_android.getWidth()) return;
            params.leftMargin = params.leftMargin + 10;
        }
        img_android.setLayoutParams(params);
    }
};
View.OnClickListener movebtnclickllistener=newview.OnClickListener(){
@凌驾
公共void onClick(视图){
ImageView img_android=(ImageView)findViewById(R.id.img_android);
RelativeLayout.LayoutParams params=(RelativeLayout.LayoutParams)img_android.getLayoutParams();
int id=view.getId();
if(id==R.id.btn_左){
如果(参数leftMargin-10<0)返回;
params.leftMargin=params.leftMargin-10;
}else if(id==R.id.btn_right){
if(params.leftMargin+10>layout_width-img_android.getWidth())返回;
params.leftMargin=params.leftMargin+10;
}
img_android.setLayoutParams(params);
}
};
在activity_main.xml中,我刚刚从图像视图中删除了“android:layout\u centerHorizontal=“true”

        <ImageView
        android:id="@+id/img_android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/buttonLinearLayout"
        android:visibility="visible"
        app:srcCompat="@mipmap/ic_launcher" />


如果我是正确的,您想在单击按钮时从左向右移动图像吗?所以这将是动画风格的简单可见按钮点击?@Target是的,你是对的,但我没有使用动画。我在ImageView.post相关的类文件上使用了setLeft()方法。您可以发布您的整个xmlI添加的main_activity.xml和MainActivity.class吗。