简体   繁体   English

查看在水平线性布局内的动态图像视图中走出屏幕

[英]View going out of screen in dynamic image views inside horizontal linear layout

I am programmatically inflating combination of ImageView and TextView inside my LinearLayout.My linear layout is under a horizontal scroll view. 我在LinearLayout中以编程方式膨胀ImageView和TextView的组合。我的线性布局在水平滚动视图下。 But when I set layout_gravity="center_horizontal" to the parent of my linear layout, the content inside linear layout going out of the screen. 但是当我将layout_gravity =“center_horizo​​ntal”设置为我的线性布局的父级时,线性布局内的内容将从屏幕中移出。

See what is happening: 看看发生了什么:

截图

I am using layout_gravity to achieve this 我正在使用layout_gravity来实现这一目标

这个

Without layout_gravity it appears like this 没有layout_gravity,它就像这样

在此输入图像描述

But if I use layout_gravity and there are many items inside that HorizontalScrollView then 1 or 2 items don't show. 但是如果我使用layout_gravity并且Horizo​​ntalScrollView中有许多项目,则不会显示1或2个项目。 You can see the first uploaded image for understanding the scenario. 您可以查看第一个上传的图像以了解该场景。 In the first image I can't scroll more towards right and I can only see see of the ImageView and TextView combination and moreover, one more such combination is out of the screen completely. 在第一张图片中,我无法向右滚动,我只能看到ImageView和TextView组合的视图,而且还有一个这样的组合完全在屏幕之外。

Here's the xml structure 这是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 manipulation 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);
    }

Isn't that what you are trying to achieve by using HorizontalScrollView ? 这不是你想通过使用HorizontalScrollView实现的吗? ScrollViews allow content to be larger than the physical display so when you add enough "hotelSpecialities" it goes out of the screen and you can scroll through it. ScrollViews允许内容大于物理显示,因此当您添加足够的“hotelSpecialities”时,它会离开屏幕,您可以滚动浏览它。 If you are trying to fit all the content on the screen then you would not use a HorizontalScrollView . 如果您尝试在屏幕上显示所有内容,则不会使用HorizontalScrollView


Okay now that I know what you are trying to achieve, putting your ScrollView inside a RelativeLayout will give you that. 好了,现在我知道你想要实现的目标,将ScrollView放在RelativeLayout会给你这个。

This is what worked out for me 这对我有用

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

You can remove the parent LinearLayout for imageViewLayout too if you don't need it. 您可以删除父LinearLayoutimageViewLayout太多,如果你不需要它。 Let me know if you need anymore help! 如果您需要帮助,请告诉我们!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使图像视图在水平滚动视图内的线性布局中居中? - How to make Image Views centre in a linear layout inside a Horizontal Scroll View? 如何制作2个图像视图,每个视图的大小可以覆盖水平滚动视图内的线性布局内的屏幕 - How to make 2 images views each be sized to cover screen inside a linear layout inside a horizontal scrollview 滚动视图内的水平和垂直线性布局 - Horizontal and Vertical linear layout inside a scroll view 在水平视图中看不到相对布局内部的线性布局 - Linear layout inside relative layout is not seen in horizontal view 将图像视图以线性布局对齐到屏幕顶部 - Align Image View to top of the screen in a linear layout 在屏幕底部使用水平滚动视图创建线性布局 - Creating Linear layout with Horizontal scroll view at the bottom of the screen 在垂直线性布局内创建动态水平线性布局 - Create dynamic horizontal linearlayout inside a vertical linear layout 线性布局超出范围 - Linear Layout going out of bounds Android线性布局图像视图并在同一水平线上编辑文本 - Android linear layout image view and edit text on the same horizontal line 线性布局中的Android自定义视图以及其他视图 - Android Custom View inside Linear Layout with other views
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM