gpt4 book ai didi

android - 在水平线性布局内的动态 ImageView 中查看屏幕外

转载 作者:太空狗 更新时间:2023-10-29 14:41:12 26 4
gpt4 key购买 nike

我在我的 LinearLayout 中以编程方式膨胀 ImageView 和 TextView 的组合。我的线性布局在水平 ScrollView 下。但是当我将 layout_gravity="center_horizo​​ntal"设置为我的线性布局的父级时,线性布局中的内容会超出屏幕。

看看发生了什么:

screenshot

我正在使用 layout_gravity 来实现这一点

this

没有 layout_gravity 它看起来像这样

enter image description here

但是如果我使用 layout_gravity 并且 Horizo​​ntalScrollView 中有很多项目,那么 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);
}

最佳答案

这不是您要通过使用 Horizo​​ntalScrollView 来实现的吗? ScrollViews 允许内容大于物理显示,因此当您添加足够多的“hotelSpecialities”时,它会离开屏幕,您可以滚动浏览它。如果您试图让所有内容都显示在屏幕上,那么您将不会使用 Horizo​​ntalScrollView


好的,现在我知道您要实现的目标了,将您的 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>

如果不需要,您也可以删除 imageViewLayout 的父级 LinearLayout。如果您需要更多帮助,请告诉我!

关于android - 在水平线性布局内的动态 ImageView 中查看屏幕外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47960064/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com