android垂直ScrollView布局,使ImageView具有固定百分比的屏幕高度
问题描述:
嗨,我是新的android开发。 我想设计一个垂直的ScrollView,它包含一个ImageView和TextView。 ImageView占据屏幕顶部50%的高度,底部部分是带有动态内容的TextView,可能会占用屏幕高度的50%以下的高度。所以ScrollView只有在文本内容占用50%以上的屏幕时才可以滚动(例如请看下面的例子) android垂直ScrollView布局,使ImageView具有固定百分比的屏幕高度
有什么办法可以实现这种布局?
答
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/scroll_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/scroll_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
这是布局。在您的活动,你应该设置ImageView
的高度为屏幕高度的50%,这样的:
ImageView scrollImage = (ImageView) findViewById(R.id.scroll_image);
TextView scrollText = (TextView) findViewById(R.id.scroll_text);
int height = getResources().getDisplayMetrics().heightPixels;
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) scrollImage.getLayoutParams();
params.height = height/2;
scrollImage.setLayoutParams(params);
希望这有助于。
答
试试下面的代码
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="100"
android:orientation="vertical">
<ImageView
android:id="@+id/scroll_image"
android:layout_width="match_parent"
android:layout_height="0dp"
android:scaleType="centerCrop"
android:layout_weight="50" />
<TextView
android:id="@+id/scroll_text"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="50" />
</LinearLayout>
</ScrollView>
尝试的LinearLayout权重和。 – Jeeva 2014-11-25 07:17:54