2012-03-23 27 views
10

Tôi có một bố trí với một HorizontalScrollView có chứa một LinearLayout cho một Menu nơi nội dung được thổi phồng với nội dung của DB. Điều này hoạt động tốt tuy nhiên khi không có đủ yếu tố để làm cho di chuyển HSV này không điền vào chiều rộng của màn hình lý tưởng nên được làm trung tâm. tức là Hiện tại:Trung tâm elemets của HorizontalScrollView khi không đủ để làm cho nó cuộn

| Element 1 Element 2       | <- edge of screen 

Thay vì:

|  Element 1   Element 2  | <- edge of screen 

trong khi vẫn có thể:

| Element 1 Element 2 Element 3 Element 4 Elem| <- edge of screen now scrolling 

Cách bố trí XML là:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainLinearLayout" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <TextView 
     android:id="@+id/header" 
     android:layout_width="fill_parent" 
     android:layout_height="25dp" > 
    </TextView> 

    <ScrollView 
     android:id="@+id/scroll1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" > 

     <LinearLayout 
      android:id="@+id/contentLayout" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" > 

     </LinearLayout> 
    </ScrollView> 

    <HorizontalScrollView 
     android:id="@+id/horizontalScrollView1" 
     android:layout_width="fill_parent" 
     android:layout_height="30dp"> 

     <LinearLayout 
      android:id="@+id/footerLayout" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="horizontal" > 

     </LinearLayout> 
    </HorizontalScrollView> 
</LinearLayout> 

Với XML sau đang phồng lên ed inside footerLayout:

<?xml version="1.0" encoding="utf-8"?> 

     <TextView 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/footer_content" 
      android:textSize="18sp" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:paddingLeft="5dp" 
      android:paddingRight="5dp" 
      android:text="FOOTER" 
      android:singleLine="true" /> 

Mọi trợ giúp sẽ được đánh giá cao.

Trả lời

0

assign wieght 1 đến TextView theo quan điểm lạm phát và làm cho chiều rộng wrapcontent

+0

không có tác dụng, vẫn có vẻ giống nhau – Alasdair

+0

nó có vẻ như các LinearLayout footerLayout không mở rộng để lấp đầy toàn bộ chiều rộng nếu chỉ có một hoặc hai yếu tố này là bên trong. – Alasdair

+0

thử một điều nữa có thể là nó hoạt động gán tổng wieght cho linearlayout trong mã ur cho bất kỳ số lượng phần tử nào mà ur chèn và gán wieght 1 trong xml cho textView được tăng cao. –

1

quản lý để có được vòng cắt trung ngang phía bên trái:

HorizontalScrollView một phần của bố cục đã được đổi thành:

<LinearLayout 
    android:id="@+id/footerWrapperLayoutToGetAroundCenteringIssue" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="50dp" 
    android:gravity="center_horizontal"> 

    <HorizontalScrollView 
     android:id="@+id/horizontalScrollView1" 
     android:layout_height="fill_parent" 
     android:layout_width="wrap_content"> 
     <LinearLayout 
      android:id="@+id/footerLayout" 
      android:orientation="horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 
     </LinearLayout> 
    </HorizontalScrollView> 
</LinearLayout> 

Chỉ là một điều đáng tiếc khi nó tập trung chúng, chúng không được phân phối đồng đều.

Nếu có ai có ý tưởng, vui lòng cho tôi biết.

22

Tôi vừa giải quyết vấn đề này. Tôi chạy vào nó một vài giờ trước. Bạn cần căn giữa HorizontalScrollView trong phần cha và đặt chiều rộng/chiều cao của nó thành wrap_content. Bố cục bạn đặt trong HSV phải có chiều rộng/chiều cao được đặt để bao bọc nội dung. Phần quan trọng ở đây là không đặt bất kỳ lực hấp dẫn/layout_gravity nào trên bố cục này hoặc bạn có thể gặp các vấn đề về cắt bớt (rất khó chịu) sau khi tăng lượt xem của bạn. Ví dụ dưới đây được chứa trong RelativeLayout.

<HorizontalScrollView android:id="@+id/svExample" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_below="@id/rlExample"> 
    <LinearLayout 
     android:id="@+id/llExample" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 
    </LinearLayout> 
</HorizontalScrollView > 
+0

Đối với tôi, nó hoạt động chính xác các thuộc tính: android: layout_centerHorizontal = "true" android: layout_width = "wrap_content" – GFPF

+0

Cảm ơn người đàn ông, bạn đã lưu ngày của tôi. – Ruslan

4

Tôi đã gặp vấn đề tương tự và cuối cùng đã làm việc đó. Dưới đây là một ví dụ nhỏ:

Cách bố trí hoạt động chính với HorizontalScrollView:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <HorizontalScrollView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:fillViewport="true" > 

     <LinearLayout 
      android:id="@+id/container" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" > 
     </LinearLayout> 

    </HorizontalScrollView> 

</LinearLayout> 

Cách bố trí của các yếu tố đó sẽ được bơm căng và đặt bên trong xem di chuyển:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_horizontal" 
    android:orientation="horizontal" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="your text" /> 

</LinearLayout> 

Và đây là ví dụ về mã để tăng thêm và thêm các phần tử:

LinearLayout container = (LinearLayout) findViewById(R.id.container); 
LayoutParams elementLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1f); 
int elementCount = 3; // ...or however many you like 
for (int i = 0; i < elementCount; i++) { 
    LinearLayout element = (LinearLayout) getLayoutInflater().inflate(R.layout.element, null); 
    container.addView(element, elementLayoutParams); 
} 

' s chủ yếu dựa trên kỹ thuật kéo dài nội dung của ScrollViews được giải thích trong this article by Romain Guy.

Tuy nhiên trong trường hợp này khi bạn đang thêm nội dung động, một điểm quan trọng khác là đặt giá trị dương cho trọng số (1f trong ví dụ này) bằng cách sử dụng LayoutParams khi thêm các phần tử vào vùng chứa. Nếu bạn có một bộ tĩnh của yếu tố mà bạn có thể đưa trực tiếp trong các thùng chứa và không cần phải thổi phồng bạn có thể xác định trọng lượng của bên ngoài LinearLayout của các yếu tố trong cách bố trí XML như sau:

android:layout_weight="1" 

Nhưng nếu bạn làm điều đó một cách năng động sẽ không hoạt động vì trọng lượng sẽ đặt lại về 0 và các phần tử sụp đổ. Do đó bạn cần phải đặt nó thông qua LayoutParams.

+0

'fillViewport =" true "' là phần còn thiếu mà tôi đang tìm kiếm để giải quyết vấn đề tương tự của tôi. Đối với người đọc trong tương lai: nếu bạn đang gặp phải sự lộn xộn bên trái thực sự của 'TextView' ở giữa' HorizontalScrollView', hãy thử tinh chỉnh với thuộc tính 'fillViewport'. Nó cho phép bật các trọng số. –

+0

bài viết của Romain Guy đã giúp tôi rất nhiều –

1

Dưới đây là cách đơn giản nhất có hiệu quả đối với tôi.

<HorizontalScrollView 
    android:id="@+id/horizontalScrollView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center"> 

    <LinearLayout 
     android:id="@+id/layout_others" 
     android:orientation="horizontal" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 
</HorizontalScrollView> 
Các vấn đề liên quan