2017-05-02 67 views
1

我有一个xml屏幕与三个孩子的线性布局垂直方向的父线性布局。第一个孩子包含一个图像视图,当运行该应用程序时,我无法查看任何图像。为什么?图像尺寸是否太大?如果是的话......如何扩展它?但是Image可以在我的设计选项卡中查看。的android stdudio。以下是我对xml文件的完整代码。如何在线性布局中居中拟合图像?

<?xml version="1.0" encoding="utf-8"?> 
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    xmlns:tools="http://schemas.android.com/tools" 
 
    android:id="@+id/activity_sign_up" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent" 
 
    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="com.example.root.my_laoder.SignUp" 
 
    android:orientation="vertical"> 
 

 
    <LinearLayout 
 
    android:layout_width="match_parent" 
 
    android:layout_height="wrap_content"> 
 

 
     <ImageView 
 
     android:layout_width="match_parent" 
 
    android:layout_height="wrap_content" 
 
    android:layout_gravity="center" 
 
    android:adjustViewBounds="true" 
 
    android:scaleType="centerInside" 
 
    android:src="@drawable/ty" 
 
     /> 
 

 
    </LinearLayout> 
 

 
    <LinearLayout 
 
     android:layout_width="match_parent" 
 
     android:layout_height="wrap_content" 
 
     android:layout_centerInParent="true" 
 
     android:orientation="vertical" 
 
     android:paddingTop="50dp" 
 

 

 
     > 
 

 

 
     <EditText 
 
      android:layout_width="match_parent" 
 
      android:layout_height="wrap_content" 
 
      android:inputType="textEmailAddress" 
 

 
      /> 
 

 
     <EditText 
 
      android:layout_width="match_parent" 
 
      android:layout_height="wrap_content" 
 
      android:inputType="textPassword" 
 

 
      /> 
 

 
     <EditText 
 
      android:layout_width="match_parent" 
 
      android:layout_height="wrap_content" 
 
      android:inputType="number" 
 

 
      /> 
 
    </LinearLayout> 
 

 
    <LinearLayout 
 
    android:layout_width="match_parent" 
 
    android:layout_height="wrap_content" 
 
     android:orientation="horizontal" 
 
     android:paddingTop="50dp" 
 
     > 
 

 
    <Button 
 
     android:text="Button" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_weight="0.5"/> 
 

 
    <Button 
 
     android:text="Button" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_weight="0.5" 
 

 
     /> 
 

 
</LinearLayout> 
 

 

 
</LinearLayout>

+0

在您的ImageView “高度和宽度”都是'wrap_content'。 –

+0

我做到了这一点,我也试着做scaletype检查我的编辑代码..没有结果:( –

+0

如果你可以提供一个快照,它会更容易对我来说。同样为了适中你的图像,你必须在包含图像视图 –

回答

2

添加属性android:gravity="center_horizontal"LinearLayout,以显示中心ImageView

2.更新ImageView宽度android:layout_width="wrap_content"和删除属性android:layout_gravity="center"。使用

android:scaleType="fitXY"代替android:scaleType="centerInside"

试试这个:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_sign_up" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal"> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:adjustViewBounds="true" 
      android:scaleType="fitXY" 
      android:src="@drawable/ty" /> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:orientation="vertical" 
     android:paddingTop="50dp"> 

     <EditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:inputType="textEmailAddress" /> 

     <EditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:inputType="textPassword" /> 

     <EditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:inputType="number" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:paddingTop="50dp"> 

     <Button 
      android:text="Button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.5" /> 

     <Button 
      android:text="Button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.5" /> 

    </LinearLayout> 
</LinearLayout> 

OUTPUT:

enter image description here

+0

假设这个ibm图像是一个很大的图像..请问这是以这种方式为中心吗?在屏幕上调整? –

+0

我想这解决了这个问题,但我的应用程序崩溃的负载说内存不足问题。我猜想我的形象太大了。如何处理这个?你能告诉吗?PLease –

+0

你可以使用Glide或Picasso加载大图。或者使用小图片作为您的标志,无需使用大图片。我已经使用500 * 400图片 – FAT

0

什么是这些参数的值梦诗?

android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 

和为什么你使用单独的linearlayout的imageView甚至父母的布局线性?

好心删除

你检查你正在使用中的ImageView背景图像的大小?如果大小大于图像可能会将你的视图的其余部分推出screen.try以给出固定的宽度和高度,假设两者都为100dp,并查看是否能够看到任何内容。

如果你想要做的ImageView中心ImagView视图

android:layout_gravity="center_horizontal" 

上面的代码行不会与ImageView的单独的线性布局工作水平使用。如果您是ImageView的要去使用不同的布局,无论如何,使用下面的代码行的LinearLayout

的Android里面:重力=“CENTER_HORIZONTAL”

注:对不起,我不能发表评论。

0

试试这个您的ImageView:

<ImageView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:adjustViewBounds="true" 
      android:scaleType="centerInside" 
      android:src="@android:drawable/btn_minus" /> 
+0

https://imagebin.ca/v/3L6C3tyxYASY这是我的设计选项卡视图..但我无法弄清楚为什么为什么我的Android应用程序显示空白而不是此标志...页面的其余部分是伟大的。 。 你能告诉??看看我已经使用了一段代码 –

+0

试图给ImageView一个固定的高度。 – Sac

+0

试过,没有发生任何事情,即使这个工程。这不会是一个好习惯。第二件事是,我甚至尝试使用不同的图像都工作正常。只是这个神奇的图像 –