为什么RelativeLayout.ALIGN_PARENT_RIGHT不起作用?

时间:2017-07-31 11:56:23

标签: android relativelayout

我的.xml:

<LinearLayout 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.example.eleizo.firstapplication.MainActivity"
android:orientation="vertical">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:id="@+id/topRL">
</RelativeLayout>
....

我在RelativeLayout中添加了自定义ImageView对象topRL.addView(map);自定义ImageView的构造函数是:

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

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

    public CustomImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,1);
        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,1);
        setBackgroundColor(Color.BLUE);
        this.setWillNotDraw(false);

        setLayoutParams(params);

    }

在创建时:

@Override
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);
    setContentView(com.example.eleizo.firstapplication.R.layout.activity_main); 

    RL = (RelativeLayout) findViewById(R.id.topRL);

    Context context = getApplicationContext();
    map = new CustomImageView(context);
    RL.addView(map,map.params);

但是RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.ALIGN_PARENT_BOTTOM不起作用:在map.setImageBitmap(input);之后,我在左上角看到了我的图片。

我希望图像的右下角位于RelativeLayout的右下角,与图像的大小无关。

我在哪里弄错了?

2 个答案:

答案 0 :(得分:0)

你的addRule错了。检查以下链接:

a)https://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html#addRule(int) b)https://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html#addRule(int,int)

如果要传递多个规则,则必须连续调用:

.addRule(RelativeLayout.ALIGN_PARENT_RIGHT) .addRule(RelativeLayout.ALIGN_PARENT_BOTTOM)

如果你想要一个代表param值的主题来设置你必须使用2参数方法addRule(int,int / bool)。

答案 1 :(得分:0)

这是适合我的解决方案。

<强> CustomeImageView.java

public class CustomImageView extends AppCompatImageView {
    public CustomImageView(Context context) {
        super(context);
        init();
    }

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

    public CustomImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
     }

    private void init() {
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        setLayoutParams(params);
    }
}

<强> main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.icpl.threadsample.MainActivity"
    tools:showIn="@layout/activity_main">

    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/black"/>

</LinearLayout>

<强> MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    final RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            CustomImageView imageView = new CustomImageView(MainActivity.this);
            imageView.setImageResource(R.mipmap.ic_launcher);
            relativeLayout.addView(imageView);
        }
    });
}
}

也可以查看此Answer