ScrollView 里面的 ImageView 应该适合设备布局
ImageView Inside the ScrollView should fit device layout
请帮我设计如下画面
我想将图像视图设置为设备布局。
例如:如果我 运行 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>
提前致谢。
您可以通过以下方式动态设置 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 });
}
您不能直接在布局 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>
以下是如何在 activity 中以编程方式设置高度。
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;
}
}
答案来源:
- Height of status bar in Android
- How to get the ActionBar height?
如果有帮助请告诉我。
请帮我设计如下画面
我想将图像视图设置为设备布局。 例如:如果我 运行 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>
提前致谢。
您可以通过以下方式动态设置 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 });
}
您不能直接在布局 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>
以下是如何在 activity 中以编程方式设置高度。
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;
}
}
答案来源:
- Height of status bar in Android
- How to get the ActionBar height?
如果有帮助请告诉我。