我的.xml:爲什麼RelativeLayout.ALIGN_PARENT_RIGHT不起作用?
<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>
....
我說我的自定義的ImageView對象的RelativeLayout 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
沒有按」 t工作:在map.setImageBitmap(input);
之後,我在左上角看到我的圖像。
我希望圖像的右下角位於RelativeLayout的右下角,與圖像的大小無關。
我在哪裏犯了錯誤?
不工作params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT); params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); – Elena
添加imageView到relativelayout後,我將位圖添加到自定義ImageView中。也許是因爲我有一些錯誤? – Elena