2013-01-07 142 views
0

我创建定制适配器(巫相对布局,我把一个ImageView的一个缩略图,一个TextView的名称和其他工作人员)画廊。调整视图的宽度和高度,以50%

画廊显示在全屏完美,但是这不是我想要..我希望自定义相对布局被缩放到在高度的50%和相对宽屏幕尺寸。我试过这个:

gallery.getLayoutParams().width = relativelayout.getWidth()/2; 
gallery.getLayoutParams().height = relativelayout.getHeight()/2; 

这是一个错误的尝试,它只是将高度和宽度设置为正确的大小而不缩放内容。

这是原来的观点:

Original

这是我所期待的:

Expectation

这就是我的了:

Result

这是布局封面:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <ImageView 
     android:id="@+id/ivTourCover" 
     android:layout_width="640dp" 
     android:layout_height="240dp" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:contentDescription="@string/app_name" 
     android:scaleType="fitXY" 
     android:src="@drawable/croppedstopgallery_f45d88e4_7864_4dbc_9123_ad7953d8724a" /> 

    <ImageButton 
     android:id="@+id/ivHorizontalLine" 
     android:layout_width="match_parent" 
     android:layout_height="1dp" 
     android:layout_below="@+id/tvTourName" 
     android:layout_marginLeft="20dp" 
     android:layout_marginRight="20dp" 
     android:layout_marginTop="0dp" 
     android:contentDescription="@string/app_name" 
     android:scaleType="fitXY" 
     android:src="@drawable/horizontal_line" /> 

    <TextView 
     android:id="@+id/tvAuthorName" 
     style="@style/Cover.AuthorName" 
     android:layout_width="fill_parent" 
     android:layout_height="19dp" 
     android:layout_alignTop="@+id/ivAuthorThumbnail" 
     android:layout_marginLeft="12dp" 
     android:layout_marginRight="17dp" 
     android:layout_toLeftOf="@+id/ivGo" 
     android:layout_toRightOf="@+id/ivAuthorThumbnail" 
     android:text="Anna Greenleaf" /> 

    <ImageView 
     android:id="@+id/ivAuthorThumbnail" 
     android:layout_width="46dp" 
     android:layout_height="46dp" 
     android:layout_alignLeft="@+id/ivHorizontalLine" 
     android:layout_below="@+id/ivHorizontalLine" 
     android:layout_marginTop="20dp" 
     android:contentDescription="@string/app_name" 
     android:scaleType="fitXY" 
     android:src="@drawable/croppedstopgallery_f45d88e4_7864_4dbc_9123_ad7953d8724a" /> 

    <ImageView 
     android:id="@+id/ivGo" 
     android:layout_width="46dp" 
     android:layout_height="46dp" 
     android:layout_alignRight="@+id/ivHorizontalLine" 
     android:layout_alignTop="@+id/tvAuthorName" 
     android:contentDescription="@string/app_name" 
     android:scaleType="fitXY" 
     android:src="@drawable/tour_cover_go" /> 

    <TextView 
     android:id="@+id/tvAuthorDescription" 
     style="@style/Cover.AuthorDescription" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/tvAuthorName" 
     android:layout_alignRight="@+id/tvAuthorName" 
     android:layout_below="@+id/tvAuthorName" 
     android:text="London native and London over!" /> 

    <SurfaceView 
     android:id="@+id/svFooter" 
     android:layout_width="wrap_content" 
     android:layout_height="30dp" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:background="@color/CoverFooter" /> 

    <TextView 
     android:id="@+id/tvFooterText" 
     style="@style/Cover.FooterText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/svFooter" 
     android:layout_alignBottom="@+id/svFooter" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="10dp" 
     android:text="Individual" /> 

    <com.example.zipdownload.utilities.ui.AutoResizeTextView 
     android:id="@+id/tvTourName" 
     style="@style/Cover.TourName" 
     android:layout_width="wrap_content" 
     android:layout_height="79dp" 
     android:layout_below="@+id/ivTourCover" 
     android:layout_centerHorizontal="true" 
     android:layout_marginLeft="20dp" 
     android:layout_marginRight="20dp" 
     android:layout_marginTop="15dp" 
     android:gravity="center|top" 
     android:text="Beautiful London in Winston Churchill&apos;s footsteps" /> 

</RelativeLayout> 

的浏览图库:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/rlGallery" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".Gallery" > 

    <Gallery 
     android:id="@+id/gCoverflow" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:gravity="top" /> 

</RelativeLayout> 

以及活动图库:

public class Gallery extends Activity { 
    private android.widget.Gallery gCoverflow; 
    private RelativeLayout rlGallery; 
    private CoverAdapter adapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_gallery); 
     rlGallery = (RelativeLayout) findViewById(R.id.rlGallery); 
     adapter = new CoverAdapter(this); 
     gCoverflow = (android.widget.Gallery) findViewById(R.id.gCoverflow); 
     gCoverflow.setAdapter(adapter); 
    } 

    @Override 
    public void onWindowFocusChanged(boolean hasFocus) { 
     super.onWindowFocusChanged(hasFocus); 
     gCoverflow.getLayoutParams().width = rlGallery.getWidth()/2; 
     gCoverflow.getLayoutParams().height = rlGallery.getHeight()/2; 
    } 
} 
+0

机器人:scaleType = “fitXY”=> “fitCenter” – njzk2

+0

我应该在哪里放呢?在画廊?或者在封面的父亲相对布局? –

+0

也,我会把宽度作为match_parent,在wrap_content的高度,adjustViewBounds在真实 – njzk2

回答

1

你可以尝试使用ScaleAnimation但在这种情况下,触摸事件将工作不正确:

final ScaleAnimation animation = new ScaleAnimation (
     1f, 0.5f, 1f, 0.5f, 
     Animation.RELATIVE_TO_SELF, 0.5f, 
     Animation.RELATIVE_TO_SELF, 0.5f 
); 

final LayoutAnimationController controller = new LayoutAnimationController(animation); 

animation.setDuration(0); 
animation.setFillAfter(true); 

gCoverflow.setLayoutAnimation(controller); 

此代码应在onCreate方法添加。

+0

对我来说工作很好,谢谢:) –

0

我发现这家伙,是代替的:

gCoverflow.getLayoutParams().width = rlGallery.getWidth()/2; 
gCoverflow.getLayoutParams().height = rlGallery.getHeight()/2; 

予用户个是:

gCoverflow.setScaleX((float) 0.5); 
gCoverflow.setScaleY((float) 0.5); 
+0

请注意'setScaleX'和'setScaleY'仅在API级别11 –

+0

是可用的,但有没有其他方法可以这样做,而无需调整布局封面的所有内容? –

相关问题