请按照以下
帮助我设计屏幕我想将imageview设置为设备布局。 例如:如果我在5英寸屏幕上运行应用程序,只显示图像视图,我需要向下滚动才能看到描述。类似地,当我在4英寸屏幕上运行应用程序时,只显示图像视图而没有任何图像切割。
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/category_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/dishDetailPageImage"
android:layout_width="match_parent"
android:layout_height="565dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"
android:src="@drawable/placeholder" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/relativeLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
//description contents
</RelativeLayout>
</ScrollView>
先谢谢。
答案 0 :(得分:3)
您无法直接在布局XML文件中设置ImageView
的高度。
您需要以编程方式找到屏幕的高度,然后需要将LayoutParams
设置为ImageView
。
以下是您需要遵循的步骤:
ActionBar
。StatusBar
。高度 - ImageView
= 1-2-3。
以下是您可以使用的XML文件。
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_red_light"
android:src="@drawable/ic_launcher" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/iv_image"
android:text="Hello how are you?, Hello how are you?, Hello how are you?,Hello how are you? ,Hello how are you? " />
</RelativeLayout>
</ScrollView>
以下是如何以编程方式在活动中设置高度。
public class MainActivity extends ActionBarActivity {
private ImageView ivImage = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = (int) (displayMetrics.heightPixels/displayMetrics.density);
int width = (int) (displayMetrics.widthPixels/displayMetrics.density);
// Calculate ActionBar height
TypedValue tv = new TypedValue();
int actionBarHeight=0;
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
{
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}
ivImage = (ImageView) findViewById(R.id.iv_image);
RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(displayMetrics.widthPixels, (displayMetrics.heightPixels-actionBarHeight-getStatusBarHeight()));
ivImage.setLayoutParams(param);
}
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
}
答案来源:
如果有帮助,请告诉我。
答案 1 :(得分:1)
您可以动态设置ImageView的高度:
ImageView yourImageView=(ImageView)v.findViewById(R.id.yourImageView);
RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(widthOfTheImage,getScreenSize(context)[1]));
获取屏幕尺寸:
public static int[] getScreenSize(Context context)
{
DisplayMetrics displayMetrics=context.getResources().getDisplayMetrics();
int screenWidthInPix = displayMetrics.widthPixels;
int screenheightInPix = displayMetrics.heightPixels;
return (new int[] { screenWidthInPix,screenheightInPix });
}