2017-07-31 15 views
0

我的.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的右下角,与图像的大小无关。

我在哪里犯了错误?

回答

0

你有你的addRule错误。检查此链接:

一)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)

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

+0

不工作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

+0

添加imageView到relativelayout后,我将位图添加到自定义ImageView中。也许是因为我有一些错误? – Elena

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以及。

+0

不幸的是不工作( – Elena