use*_*474 5 android android-layout android-imageview android-framelayout
以下工作,但我想消除xml,以便我可以编程方式更改图像:Java:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.p6_touch);
ImageView floatImage = (ImageView) findViewById(R.id.p6_imageView);
floatImage.setOnTouchListener(this);
}
Run Code Online (Sandbox Code Playgroud)
XML:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/background_black" >
<ImageView
android:id="@+id/p6_imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="matrix"
android:src="@drawable/p6_trail_map" >
</ImageView>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:onClick="showFlashlight"
android:src="@drawable/legend_block24" />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
在文档中有一个将XML映射到Java 的表.
android:src相当于setImageResource().您需要检查继承表中是否有来自任何超类的属性.
android:id相当于setId().width,, height和gravity都在LayoutParams对象中设置并传递给setLayoutParams().
了解并非每个XML属性都具有匹配的Java方法(反之亦然),但您使用的所有属性都是如此.
一个例子,让我们调用这个文件activity_main.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/background_black" >
<!-- We'll add this missing ImageView back in with Java -->
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:onClick="showFlashlight"
android:src="@drawable/legend_block24" />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
现在我们的活动:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Let's create the missing ImageView
ImageView image = new ImageView(this);
// Now the layout parameters, these are a little tricky at first
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT);
image.setScaleType(ImageView.ScaleType.MATRIX);
image.setImageResource(R.drawable.p6_trail_map);
image.setOnTouchListener(this);
// Let's get the root layout and add our ImageView
FrameLayout layout = (FrameLayout) findViewById(R.id.root);
layout.addView(image, 0, params);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
28611 次 |
| 最近记录: |