我在LinearLayout中以编程方式膨胀ImageView和TextView的组合。我的线性布局在水平滚动视图下。但是当我设置layout_gravity =" center_horizontal"到我的线性布局的父级,线性布局内的内容走出屏幕。
看看发生了什么:
我正在使用layout_gravity来实现这个
没有layout_gravity,它就像这样
但是如果我使用layout_gravity并且HorizontalScrollView中有许多项目,那么1或2个项目不会显示。您可以查看第一个上传的图像以了解该场景。在第一张图片中,我无法向右滚动,我只能看到ImageView和TextView组合,而且还有一个这样的组合完全在屏幕外。
这里是xml结构
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbarAlwaysDrawHorizontalTrack="false"
android:scrollbars="none"
android:id="@+id/hotel_detail_services">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/imageViewLayout"
>
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
Java操作
@Override
protected void onPostExecute(List<hotel_detail_model> hotel_detail_models) {
super.onPostExecute(hotel_detail_models);
LinearLayout layout = findViewById(R.id.imageViewLayout);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setGravity(Gravity.CENTER_HORIZONTAL);
if (hotel_detail_models.get(0).getTv()==1)
addHotelSpeciality(layout,"TV",R.drawable.tv);
if (hotel_detail_models.get(0).getAc()==1)
addHotelSpeciality(layout,"AC",R.drawable.ac);
if (hotel_detail_models.get(0).getGeyser()==1)
addHotelSpeciality(layout,"Geyser",R.drawable.geyser);
if (hotel_detail_models.get(0).getBus()==1)
addHotelSpeciality(layout,"BUS",R.drawable.bus);
if (hotel_detail_models.get(0).getCab()==1)
addHotelSpeciality(layout,"CAB",R.drawable.cab);
if (hotel_detail_models.get(0).getFood()==1)
addHotelSpeciality(layout,"FOOD",R.drawable.food);
if (hotel_detail_models.get(0).getRailway()==1)
addHotelSpeciality(layout,"Train",R.drawable.train);
if (hotel_detail_models.get(0).getAirport()==1)
addHotelSpeciality(layout,"Airport",R.drawable.airport);
if (hotel_detail_models.get(0).getMedical()==1)
addHotelSpeciality(layout,"Medical",R.drawable.medical);
progressDialog.dismiss();
}
//Add Image Dynamically
private void addHotelSpeciality(LinearLayout layout, String image_name, int image_id) {
LinearLayout linearLayout = new LinearLayout(hotel_details.this);
ImageView image = new ImageView(hotel_details.this,null,R.style.dynamicImageHotel);
TextView textView = new TextView(hotel_details.this,null,R.style.dynamicTextHotel);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(getResources().getDimensionPixelSize(R.dimen._25sdp),
getResources().getDimensionPixelSize(R.dimen._25sdp));
params.weight = 1.0f;
params.gravity = Gravity.CENTER_HORIZONTAL;
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params2.weight = 1.0f;
params2.gravity = Gravity.CENTER_HORIZONTAL;
LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
params3.weight = 1.0f;
params2.gravity = Gravity.CENTER_HORIZONTAL;
image.setLayoutParams(params);
image.setScaleType(ImageView.ScaleType.FIT_CENTER);
textView.setLayoutParams(params2);
image.setImageDrawable( getResources().getDrawable(image_id));
textView.setText(image_name);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
textView.setTextAppearance(R.style.dynamicTextHotel);
}else {
textView.setTextAppearance(hotel_details.this,R.style.dynamicTextHotel);
}
// Adds the view to the layout
linearLayout.addView(image);
linearLayout.addView(textView);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(params3); linearLayout.setPadding(getResources().getDimensionPixelSize(R.dimen._15sdp),0,0,0);
layout.addView(linearLayout);
}
答案 0 :(得分:0)
使用HorizontalScrollView
并不是您想要实现的目标吗? ScrollViews允许内容大于物理显示,所以当你添加足够的&#34; hotelSpecialities&#34;它离开了屏幕,你可以滚动它。如果您尝试在屏幕上显示所有内容,则不会使用HorizontalScrollView
。
现在好了,我知道你想要实现的目标,将你的ScrollView
放在RelativeLayout
内即可。
这对我有用
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<HorizontalScrollView
android:id="@+id/hotel_detail_services"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:scrollbarAlwaysDrawHorizontalTrack="false"
android:scrollbars="none">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/imageViewLayout"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
如果您不需要LinearLayout
,也可以移除imageViewLayout
的父{{1}}。如果您需要帮助,请告诉我们!