如何以编程方式将我的 imageView center_veritically 放在 FrameLayout 上?
How can I put programmatically my imageView center_veritically on a FrameLayout?
我想在 FrameLayout 的 center_vertically 上放一个 imageView。但是,我做不到。我试过 FrameLayout.LayoutParams 但是,它没有 .addRule() 方法。
public class P023Deneme extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout frameLayout=new FrameLayout(this);
ImageView imageViewBackgroud=new ImageView(this);
imageViewBackgroud.setImageResource(R.drawable.sample_6);
ImageView imageViewLeftArrow=new ImageView(this);
imageViewLeftArrow.setImageResource(R.drawable.slidersolok_logo);
frameLayout.addView(imageViewBackgroud);
frameLayout.addView(imageViewLeftArrow,50,50);
setContentView(frameLayout);
}
}
在 XML 中完成,而不是在代码中,它更容易
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="@drawable/background_drawable"/>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/arrow_drawable"/>
</FrameLayout>
如果您仍想以编程方式执行此操作,则:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout frameLayout=new FrameLayout(this);
ImageView imageViewBackgroud=new ImageView(this);
imageViewBackgroud.setImageResource(R.drawable.background);
ImageView imageViewLeftArrow=new ImageView(this);
imageViewLeftArrow.setImageResource(R.drawable.arrow);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER_VERTICAL);
frameLayout.addView(imageViewBackgroud);
frameLayout.addView(imageViewLeftArrow, layoutParams);
setContentView(frameLayout);
}
我想在 FrameLayout 的 center_vertically 上放一个 imageView。但是,我做不到。我试过 FrameLayout.LayoutParams 但是,它没有 .addRule() 方法。
public class P023Deneme extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout frameLayout=new FrameLayout(this);
ImageView imageViewBackgroud=new ImageView(this);
imageViewBackgroud.setImageResource(R.drawable.sample_6);
ImageView imageViewLeftArrow=new ImageView(this);
imageViewLeftArrow.setImageResource(R.drawable.slidersolok_logo);
frameLayout.addView(imageViewBackgroud);
frameLayout.addView(imageViewLeftArrow,50,50);
setContentView(frameLayout);
}
}
在 XML 中完成,而不是在代码中,它更容易
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="@drawable/background_drawable"/>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/arrow_drawable"/>
</FrameLayout>
如果您仍想以编程方式执行此操作,则:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout frameLayout=new FrameLayout(this);
ImageView imageViewBackgroud=new ImageView(this);
imageViewBackgroud.setImageResource(R.drawable.background);
ImageView imageViewLeftArrow=new ImageView(this);
imageViewLeftArrow.setImageResource(R.drawable.arrow);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER_VERTICAL);
frameLayout.addView(imageViewBackgroud);
frameLayout.addView(imageViewLeftArrow, layoutParams);
setContentView(frameLayout);
}