簡體   English   中英

如何在水平滾動視圖中更改圖像按鈕的寬度?

[英]How can I change the width of an image button in a horizontal scroll view?

我正在嘗試創建一個看起來像 GitHub 貢獻頁面的頁面(比如每個方塊代表一天,方塊的透明度代表提交次數)。 我希望頁面看起來像這樣:

在此處輸入圖片說明

所以我做了一個水平滾動視圖,因為我希望方塊部分可以水平滾動。 我嘗試將方塊添加到 HSV 中。 這是代碼:

<HorizontalScrollView
    android:id="@+id/scrollView"
    android:layout_width="0dp"
    android:layout_height="295dp"
    android:layout_marginTop="10dp"
    android:layout_marginRight="60dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/time">

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="20dp"  <!-- the problem -->
        android:layout_height="35dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:background="@drawable/square" />

</HorizontalScrollView>

問題是,我無法設置圖像按鈕的layout_width,不管我怎么改,設計總是這樣:

在此處輸入圖片說明

如您所見,高度是正確的,但寬度固定在一個奇怪的地方。 無論如何我可以改變它嗎?

任何幫助,將不勝感激。

您應該使用 Horizo​​ntalScrollView 內部的布局,而不是直接添加圖像按鈕。 例如 LinearLayout 或 GridLayout

重點是滾動視圖只能有一個孩子,因為滾動視圖擴展了FrameLayout

文檔中引用:

可以由用戶滾動的視圖層次結構的布局容器,允許它大於物理顯示。 Horizo​​ntalScrollView 是一個 FrameLayout,這意味着您應該在其中放置一個包含要滾動的全部內容的子項; 這個孩子本身可能是一個具有復雜對象層次結構的布局管理器。 經常使用的子元素是水平方向的 LinearLayout,呈現用戶可以滾動瀏覽的頂級項目的水平數組。

暫無
暫無

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

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