2015-10-23 174 views
0

問候。我使用的是this ChipView library爲TextView的芯片被點擊一個ListView項目時,芯片將被添加到ChipView:android:chipview上的水平滾動視圖

enter image description here

我需要一個水平滾動型的ChipView,但它只是「縮水」,可以」噸被照常顯示當我與Horizo​​ntalScrollView標籤包裝它:

enter image description here

它工作正常的(垂直)滾動型: enter image description here

我該如何顯示水平滾動的ChipView?

我的XML(ll_searchBar2):

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

<LinearLayout 
    android:id="@+id/ll_searchBar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:orientation="horizontal"> 

     <ImageView 
      android:layout_width="50dp" 
      android:layout_height="50dp" 
      android:padding="10dp" 
      android:scaleType="centerCrop" 
      android:src="@drawable/icon_search" /> 

     <!-- Dummy item to prevent EditText from receiving focus --> 
     <LinearLayout 
      android:layout_width="0px" 
      android:layout_height="0px" 
      android:focusable="true" 
      android:focusableInTouchMode="true" /> 

     <EditText 
      android:id="@+id/ed_searchField" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:hint="Search by Name or Email Address" 
      android:imeOptions="actionSearch" 
      android:inputType="text" 
      android:padding="10dp" /> 

    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/ll_searchBar2" 
     android:layout_width="match_parent" 
     android:layout_height="70dp" 
     android:layout_alignParentTop="true" 
     android:orientation="vertical"> 

     <HorizontalScrollView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <com.gamification.gamificationpagestudy.chipview.ChipView 
       android:id="@+id/text_my_chip" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="#ffe0d9" /> 

     </HorizontalScrollView> 

    </LinearLayout> 

</LinearLayout> 

<LinearLayout 
    android:id="@+id/llBottomAction" 
    android:layout_width="match_parent" 
    android:layout_height="48dp" 
    android:layout_alignParentBottom="true" 
    android:background="#c6b0ff" 
    android:gravity="center" 
    android:orientation="horizontal" 
    android:visibility="visible"> 

    <ImageView 
     android:layout_width="32dp" 
     android:layout_height="32dp" 
     android:scaleType="centerCrop" 
     android:src="@drawable/abc_ratingbar_full_material" /> 

    <TextView 
     android:id="@+id/txtInvite" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@color/transparent" 
     android:paddingLeft="8dp" 
     android:text="INVITE (0)" 
     android:textColor="#ffffff" /> 

</LinearLayout> 

<ListView 
    android:id="@+id/invitationListView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_above="@id/llBottomAction" 
    android:layout_below="@id/ll_searchBar" 
    android:background="#ffffff" 
    android:divider="@android:color/transparent" 
    android:scrollbarStyle="outsideOverlay" /> 

我的活動:

public class FourteenthActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_twelveth); 

    ChipView chipView = (ChipView) findViewById(R.id.text_my_chip); 
    chipView.add(new Tag("chip1")); 
    chipView.add(new Tag("chip2")); 
    chipView.add(new Tag("chip3")); 
    chipView.add(new Tag("chip4")); 
    chipView.add(new Tag("chip5")); 
    chipView.add(new Tag("chip6")); 
    chipView.add(new Tag("chip7")); 
    chipView.add(new Tag("chip8")); 
    chipView.add(new Tag("chip9")); 
    chipView.add(new Tag("chip10")); 

} 
public class Tag implements Chip { 
    private String mName; 
    private int mType = 0; 

    public Tag(String name, int type) { 
     this(name); 
     mType = type; 
    } 

    public Tag(String name) { 
     mName = name; 
    } 

    @Override 
    public String getText() { 
     return mName; 
    } 

    public int getType() { 
     return mType; 
    } 
} 
} 

回答

0

採取的LinearLayout作爲根佈局。然後以編程方式添加ChipViews。

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100 ,100); 
ChipView chipView = new ChipView(context); 
layoutParams.setMargins(0, 0, 10, 0); 
chipView.setLayoutParams(layoutParams); 
linearLayuot.addView(chipView); 
+0

感謝您的回覆。即使在程序中手動設置寬度和高度,當芯片達到寬度的末端時,芯片也會到達下一行,但水平滾動視圖仍然不起作用。 –

+0

將LinearLayout方向設置爲「水平」。 – activesince93

+0

檢查此Git存儲庫:https://github.com/activesince93/CustomViews 你會得到一些想法。只需將LinearLayout方向設置爲「水平」即可。 – activesince93