2013-06-30 99 views
0

我的问题对于其他人来说可能是微不足道的,但对我而言,我无法使其工作。我有布局xml如下。将ImageView设置在屏幕中央

<?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:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    > 

    <LinearLayout android:id="@+id/tracker_container" 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1" 
     android:orientation="vertical" 
     /> 

    <ImageView 
     android:id="@+id/mainImageView" 
     android:contentDescription="@string/display"   
     android:layout_gravity="center" 
     android:layout_width="400dp" 
     android:layout_height="300dp" 
     android:opacity="translucent" 

     /> 

</LinearLayout> 

我怎样才能让ImageView是在整个屏幕的中心? 感谢

回答

0

尝试this--

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLaout 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" 
> 

<RelativeLayout android:id="@+id/tracker_container" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
/> 

<ImageView 
    android:id="@+id/mainImageView" 
    android:contentDescription="@string/display"   
    android:layout_centerInParent="true" 
    android:layout_width="400dp" 
    android:layout_height="300dp" 
    android:opacity="translucent" 

    /> 

</RelativeLayout> 
+0

android:layout_centerInParent =“true”可以替换为android:layout_centerHorizo​​ntal =“true”和android:layout_centerVertical =“true” –

0

您可以使用RelativeLayout作为家长和使用layout_centerInParent

<RelativeLayout 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"> 

    <LinearLayout android:id="@+id/tracker_container" 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1" 
     android:orientation="vertical" /> 

    <ImageView 
     android:id="@+id/mainImageView" 
     android:contentDescription="@string/display"   
     android:layout_centerInParent="true" 
     android:layout_width="400dp" 
     android:layout_height="300dp" 
     android:opacity="translucent" /> 

</RelativeLayout> 

我不知道这是什么LinearLayout是,但我已经在那里保留了它。也许你是以编程方式填充它,它会推动你的下降。 RelativeLayout确保它始终是父级的核心,无论其他视图元素如何。

+0

的LinearLayout是ListView控件。我曾尝试过RelativeLayout。问题是ImageView被设置在屏幕的中心。但是ListView没有出现。 – batuman

+0

请condider发布您导致问题的实际代码。 :) –