Jak dodać (pionowy) dzielnik do linii poziomej?

Próbuję dodać dzielnik do poziomego układu liniowego, ale nigdzie nie zmierzam. Dzielnik po prostu się nie pokazuje. Jestem totalnym nowicjuszem z Androidem.

To jest mój layout XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/llTopBar"
        android:orientation="horizontal"
        android:divider="#00ff00"
        android:dividerPadding="22dip"
        android:showDividers="middle"
       >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="asdf" />
            <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="asdf"
             />

    </LinearLayout>

</RelativeLayout>
Author: Jonik, 2013-02-28

12 answers

Użyj tego do dzielnika poziomego

<View
    android:layout_width="1dp"
    android:layout_height="match_parent"
    android:background="@color/honeycombish_blue" />

I to dla dzielnika pionowego

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@color/honeycombish_blue" />

Lub jeśli możesz użyć dzielnika LinearLayout, dla dzielnika poziomego

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <size android:height="1dp"/>
    <solid android:color="#f6f6f6"/>
</shape>

I w LinearLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="@drawable/divider"
    android:orientation="vertical"
    android:showDividers="middle" >

Jeśli chcesz użyć dzielnika pionowego, to zamiast android:height="1dp" W kształcie użyj android:width="1dp"

Wskazówka: nie zapomnij elementu android:showDividers.

 186
Author: Kapil Vats,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-13 14:54:33

Spróbuj tego, utwórz dzielnik w folderze res/drawable:

Vertical_divider_1.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">    
    <size android:width="1dip" />
    <solid android:color="#666666" />    
</shape> 

I użyj atrybutu divider w LinearLayout w następujący sposób:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:orientation="horizontal"
    android:divider="@drawable/vertical_divider_1"
    android:dividerPadding="12dip"
    android:showDividers="middle"
    android:background="#ffffff" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

Uwaga: android:divider jest dostępny tylko w systemie Android 3.0 (poziom API 11) lub wyższym.

 64
Author: Shreesha S,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-12-30 10:35:47

Łatwo jest dodać dzielnik do układu, nie potrzebujemy osobnego widoku.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:divider="?android:listDivider"
    android:dividerPadding="2.5dp"
    android:orientation="horizontal"
    android:showDividers="middle"
    android:weightSum="2" ></LinearLayout>

Powyższy kod tworzy pionowy dzielnik dla LinearLayout

 29
Author: khaintt,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-10-21 06:51:33

Update: pre-Honeycomb using AppCompat

Jeśli używasz biblioteki AppCompat v7, możesz użyć widoku LinearLayoutCompat. Korzystając z tego podejścia, możesz użyć rysowalnych dzielników na Androidzie 2.1, 2.2 i 2.3.

Przykładowy kod:

<android.support.v7.widget.LinearLayoutCompat
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:showDividers="middle"
        app:divider="@drawable/divider">

Drawable / divider.xml: (dzielnik z wypełnieniem na górze i na dole)

<?xml version="1.0" encoding="UTF-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
        android:insetBottom="2dp"
        android:insetTop="2dp">
    <shape>
        <size android:width="1dp" />
        <solid android:color="#FFCCCCCC" />
    </shape>
</inset>

Bardzo Ważna uwaga: widok LinearLayoutCompat nie rozszerza LinearLayout i dlatego nie należy używać android:showDividers lub android:divider właściwości, ale te niestandardowe: app:showDividers i app:divider. W kodzie należy również użyć LinearLayoutCompat.LayoutParams, a nie LinearLayout.LayoutParams!

 15
Author: Rolf ツ,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-04 20:24:19

Wpadłam dzisiaj na ten sam problem. Jak wskazują poprzednie odpowiedzi, problem wynika z użycia koloru w znaczniku dzielnika, a nie drawable. Jednak zamiast pisać własny rysowalny xml, wolę używać atrybutów tematycznych w jak największym stopniu. Możesz użyć android: attr / dividerHorizontal i android: attr / dividerVertical, aby uzyskać predefiniowany drawable zamiast:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:showDividers="middle"
    android:divider="?android:attr/dividerVertical"
    android:orientation="horizontal">
    <!-- other views -->
</LinearLayout>

Atrybuty są dostępne w API 11 i wyżej.

Również, jak wspomniał bocekm w jego odpowiedzi właściwość dividerPadding nie dodaje dodatkowego padding po obu stronach pionowego dzielnika, jak można założyć. Zamiast tego definiuje górną i dolną wyściółkę, a tym samym może obciąć dzielnik, jeśli jest zbyt duży.

 8
Author: Nicolai Buch-Andersen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-03-31 16:55:13

Frustrująco, musisz włączyć wyświetlanie dzielników z kodu w swojej aktywności. Na przykład:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Set the view to your layout
    setContentView(R.layout.yourlayout);

    // Find the LinearLayout within and enable the divider
    ((LinearLayout)v.findViewById(R.id.llTopBar)).
        setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);

}
 4
Author: dougc,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-06-28 22:04:32

Możesz użyć wbudowanego dzielnika, będzie to działać dla obu orientacji.

<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:divider="?android:attr/listDivider"
  android:orientation="horizontal"
  android:showDividers="middle">
 4
Author: Amilcar Andrade,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-29 16:53:21

Twój dzielnik może nie być wyświetlany z powodu zbyt dużego dzielnika. Ustawiasz 22dip, co oznacza, że dzielnik jest obcinany o 22dip od góry i o 22dip od dołu. Jeśli wysokość układu jest mniejsza lub równa 44dip, nie jest widoczny żaden dzielnik.

 1
Author: bocekm,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-05-15 16:09:29

Musisz utworzyć dowolny widok dla separatera, takiego jak textview lub imageview, a następnie ustawić tło dla tego, jeśli masz obraz, użyj koloru jako tła.

Mam nadzieję, że to ci pomoże.

 0
Author: itsrajesh4uguys,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-02-28 06:20:25

Aby zostać narysowanym, dzielnik LinearLayout musi mieć pewną wysokość, podczas gdy ColorDrawable (który jest zasadniczo #00ff00, jak również każdy inny kolor na twardo) nie ma. Prostym (i poprawnym) sposobem rozwiązania tego problemu jest zawinięcie koloru w jakieś {[3] } z predefiniowaną wysokością, np. shape drawable

 0
Author: Dmitry Zaytsev,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-01-06 17:47:31

Możesz użyć IcsLinearLayout, używanego w Bibliotece ActionBarSherlock, lub użyć normalnego API dostępnego w ICS (lub skopiować jego kod).

EDIT: Możesz również użyć LinearLayoutICS, jak jest to używane w bibliotece wsparcia. Napisałam o tym posta Proszę..

Możesz użyć tej samej techniki, aby utworzyć drawable, jak pokazano Proszę., lub utworzyć 9-patch drawable.

 0
Author: android developer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:10:39

Jeśli odpowiedź kapilary nie działa spróbuj czegoś takiego:

Drawable/divider_horizontal_green_22.xml

    <size android:width="22dip"/>
    <solid android:color="#00ff00"/>

</shape>

Layout / your_layout.xml

LinearLayout 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/llTopBar"
            android:orientation="horizontal"
            android:divider="@drawable/divider_horizontal_green_22"
            android:showDividers="middle"
           >

Napotkałem problem, w którym atrybut padding nie działał, dlatego musiałem ustawić wysokość dzielnika bezpośrednio w dzielniku.

Uwaga:

Jeśli chcesz go używać w LinearLayout, zrób nowy, jak to: drawable / divider_vertical_green_22.xml

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

    <size android:height="22dip"/>
    <solid android:color="#00ff00"/>

</shape>
 0
Author: Ionut Negru,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:10:39