尝试在图像视图上设置图像,它将根据屏幕尺寸拉伸图像,并且还会自动调整图像,使其不会超出屏幕。
我的主要活动:
package com.example.testting;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout ll = (LinearLayout) findViewById(R.id.llLayout);
for(int i=0;i<10;i++)
{
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(R.dimen.num_icon, R.dimen.num_icon);
ImageView ii= new ImageView(this);
ii.setBackgroundResource(R.drawable.apple);
ii.setLayoutParams(layoutParams);
ll.addView(ii);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
我的 XML:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:id="@+id/llLayout"
android:orientation="horizontal"
android:gravity="center" >
</LinearLayout>
我res/values/dimens
的标准手机屏幕文件:
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="num_icon">55dp</dimen>
</resources>
我res/values/dimens
的 7" 屏幕文件:
<resources>
<!--
Customize dimensions originally defined in res/values/dimens.xml (such as
screen margins) for sw600dp devices (e.g. 7" tablets) here.
-->
<dimen name="num_icon">110dp</dimen>
</resources>
我res/values/dimens
的 10" 屏幕文件:
<resources>
<!--
Customize dimensions originally defined in res/values/dimens.xml (such as
screen margins) for sw720dp devices (e.g. 10" tablets) here.
-->
<dimen name="num_icon">160dp</dimen>
</resources>
它应该使用维度 XML 显示 10 个图像,但事实并非如此。我看到以下内容: