[英]Linear Layout or Horizontal Scroll View is cutting off content
我正在嘗試實現一個簡單的屏幕,其中我有一個帶有書籍條目的水平滾動視圖。 我遇到的問題是它似乎過早地切斷,切斷條目。 我查看了與此類似的其他問題,他們的修復似乎無法解決我的問題,因此我不確定如何處理。
這是切斷的樣子:
這是我的代碼:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#D3D3D3" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:orientation="vertical" >
<TextView
android:id="@+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:text="@string/af"
android:textAppearance="?android:attr/textAppearanceLarge" />
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#A1A1A1" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill_horizontal|end"
android:orientation="horizontal" >
<RelativeLayout
android:id="@+id/relative1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="168dp"
android:layout_height="258dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:contentDescription="@string/cd"
android:src="@drawable/af1" />
<TextView
android:id="@+id/textLabel1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/imageView1"
android:layout_centerInParent="true"
android:singleLine="true"
android:text="Guilty Wives" />
</RelativeLayout>
/*RelativeLayout repeats with different data, cut for brevity*/
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</ScrollView>
這是一個從未解決的已知錯誤:( https://code.google.com/p/android/issues/detail?id=20088 ),但這是對我有用的代碼。 訣竅是設置水平滾動視圖的寬度以包裝內容,並設置其下方的線性(或相對)布局的寬度以匹配 parent 。
<HorizontalScrollView android:id="@+id/group_scroller" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioGroup android:id="@+id/choices_group" android:layout_width="wrap_content" android:orientation="horizontal" android:paddingTop="4dp" android:paddingBottom="4dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:gravity="center_horizontal" /> </LinearLayout> </HorizontalScrollView>
我設法通過將android:paddingRight="150dip"
到 LinearLayout 來修復它。
話雖如此,我猜這個修復程序很笨拙,實際上並沒有解決最初的問題。
我嘗試了我在網上能找到的所有解決方案,但沒有一個適用於 HorizontalScrollView 中的相對布局,所以我所做的只是一種解決方法。 在布局中添加我的所有視圖后,我在作為布局上的最后一個孩子之后添加了一條不可見的“線”,並將其放置在我最后一個實際視圖的右側。 這有點 hacky,但它是在不實現不必要的自定義視圖的情況下最不復雜和最有效的方法。
View shim = new View(sender.getApplicationContext());
RelativeLayout.LayoutParams shimParams = new RelativeLayout.LayoutParams(0,ViewGroup.LayoutParams.MATCH_PARENT);
shimParams.addRule(RelativeLayout.RIGHT_OF,yourViewIDHere);
shim.setLayoutParams(shimParams);
YourLayout.addView(shim);
就像一個魅力,如果有人在尋找有用的東西時偶然發現它,希望這會有所幫助。
刪除線性布局中的 android:layout_gravity="center_horizontal"。
<HorizontalScrollView
android:layout_gravity="center_horizontal"
android:scrollbars="none"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/linear_buttons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
......
......
</LinearLayout>
</HorizontalScrollView>
我正在使用
android:layout_gravity="center"
在我的線性布局中。 刪除了它,它工作正常
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.