簡體   English   中英

查看在水平線性布局內的動態圖像視圖中走出屏幕

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

我在LinearLayout中以編程方式膨脹ImageView和TextView的組合。我的線性布局在水平滾動視圖下。 但是當我將layout_gravity =“center_horizo​​ntal”設置為我的線性布局的父級時,線性布局內的內容將從屏幕中移出。

看看發生了什么:

截圖

我正在使用layout_gravity來實現這一目標

這個

沒有layout_gravity,它就像這樣

在此輸入圖像描述

但是如果我使用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);
    }

這不是你想通過使用HorizontalScrollView實現的嗎? ScrollViews允許內容大於物理顯示,因此當您添加足夠的“hotelSpecialities”時,它會離開屏幕,您可以滾動瀏覽它。 如果您嘗試在屏幕上顯示所有內容,則不會使用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>

您可以刪除父LinearLayoutimageViewLayout太多,如果你不需要它。 如果您需要幫助,請告訴我們!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM