Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在选中时制作分割视图并显示彩色的布局?_Java_Android_Layout_Colors_Selector - Fatal编程技术网

Java 如何在选中时制作分割视图并显示彩色的布局?

Java 如何在选中时制作分割视图并显示彩色的布局?,java,android,layout,colors,selector,Java,Android,Layout,Colors,Selector,我想做一个布局,其中有不同的选项,当我们选择它应该看起来彩色。如下图所示: 我试过这样的方法,但效果不一样。我尝试使用选择器在单击时将其显示为彩色,但单击后颜色会消失。它只显示我们按下视图的时间 请帮忙。。我想使相同的布局像图像 <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="50dp" and

我想做一个布局,其中有不同的选项,当我们选择它应该看起来彩色。如下图所示:

我试过这样的方法,但效果不一样。我尝试使用选择器在单击时将其显示为彩色,但单击后颜色会消失。它只显示我们按下视图的时间

请帮忙。。我想使相同的布局像图像

 <LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="05dp"
    android:layout_marginRight="05dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/shape">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@drawable/shape"
        android:clickable="true">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="@string/pickupbak"
        android:id="@+id/textView47"
        android:layout_gravity="center"
        android:textAlignment="gravity"
        android:gravity="center_vertical"
        android:layout_marginLeft="05dp"
        android:layout_marginRight="05dp" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@drawable/shape">
    <TextView
        android:layout_width="89dp"
        android:layout_height="match_parent"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="@string/pickupbox"
        android:id="@+id/textView48"
        android:layout_gravity="center"
        android:gravity="center_vertical"
        android:layout_marginLeft="05dp"
        android:layout_marginRight="05dp" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@drawable/shape">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="@string/engkelbak"
        android:id="@+id/textView49"
        android:layout_gravity="center"
        android:gravity="center_vertical"
        android:layout_marginLeft="05dp"
        android:layout_marginRight="05dp" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@drawable/shape">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="@string/engkelbox"
        android:id="@+id/textView50"
        android:layout_gravity="center"
        android:gravity="center_vertical"
        android:layout_marginLeft="05dp"
        android:layout_marginRight="05dp" />
        </LinearLayout>
</LinearLayout>
</LinearLayout>

选择器

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
        android:drawable="@android:drawable/list_selector_background" />
</selector>

如我对3个选项卡的评论所述: 我正在与您共享代码:

对于线性布局:可绘制背景:bg_home_buttons.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="@color/blue"/>
    <stroke android:width="@dimen/stroke_width" android:color="@color/blue" />
    <corners android:radius="@dimen/five"/>
</shape>
在changeBackground方法中,您应该从onClickListener传递单击按钮id的id

尺寸:

<resources>
    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>

    <!-- Per the design guidelines, navigation drawers should be between 240dp and 320dp:
         https://developer.android.com/design/patterns/navigation-drawer.html -->
    <dimen name="navigation_drawer_width">300dp</dimen>
    <dimen name="padding_text_view">10dp</dimen>
    <dimen name="border">1dp</dimen>
    <dimen name="ten">10dp</dimen>
    <dimen name="five">5dp</dimen>
    <dimen name="radius_circle_indicator">8dp</dimen>
    <dimen name="width_button_sign_up">300dp</dimen> -2px match_parent
    <dimen name="size_sign_up_text">13sp</dimen>  15
    <dimen name="padding_tutorial_text">20dp</dimen>
    <dimen name="padding_text_view_right">30dp</dimen>
    <dimen name="fifteen_sp">14sp</dimen> 15
    <dimen name="eighteen_sp">15sp</dimen> 18
    <dimen name="twenty_sp">17sp</dimen> 20
    <dimen name="two">2dp</dimen>
    <dimen name="one">0.5dp</dimen>
    <dimen name="sixteen_sp">16sp</dimen>
    <dimen name="twenty_five_sp">22sp</dimen> 25
    <dimen name="margin_">30dp</dimen>
    <dimen name="map_padding">100dp</dimen>
    <dimen name="stroke_width">1dp</dimen>
    <dimen name="loader_size">80dp</dimen>
    <dimen name="loading_width">5dp</dimen>
    <dimen name="loader_layout_size">300dp</dimen>
    <dimen name="fifteen">15dp</dimen>
    <dimen name="fourty_sp">30sp</dimen> 40
    <dimen name="thirty_sp">20sp</dimen> 20
    <dimen name="height_chart">230dp</dimen>
    <dimen name="height_chart_minus">200dp</dimen>
    <dimen name="width_chart">400dp</dimen>
    <dimen name="size_sales_history">11sp</dimen> 13
    <dimen name="size_text_house_price">22sp</dimen> 25
    <dimen name="twenty">20dp</dimen>
    <dimen name="fifty">100dp</dimen>
    <dimen name="top_margin_sales_history_button">30dp</dimen>
    <dimen name="ten_minus">-10dp</dimen>
    <dimen name="size_marker_text">11sp</dimen>
    <dimen name="profile_image_size">120dp</dimen>
    <dimen name="invite_icon_size">18sp</dimen>
    <dimen name="fourty">40dp</dimen>
    <dimen name="minimum_height_listview">200dp</dimen>
    <dimen name="map_size">300dp</dimen>
    <dimen name="bar_text_size">11sp</dimen>
    <dimen name="margin_bar_text">7dp</dimen>
    <dimen name="five_hundred">800dp</dimen>
    <dimen name="padding_text_view_fourteen">14dp</dimen>
    <dimen name="eight">8dp</dimen>
    <dimen name="thirteen">13dp</dimen>
    <dimen name="three">3dp</dimen>
    <dimen name="seven">7dp</dimen>
    <dimen name="margin_home_buttons">14dp</dimen>
    <dimen name="margin_home_buttons_four">4dp</dimen>
    <dimen name="space_to_change_since">3dp</dimen>

    <dimen name="half_divider">0.5dp</dimen>
    <dimen name="size_sales_history_box_width" >60dp</dimen>
    <dimen name="height_line_thick">@dimen/two</dimen>
    <dimen name="height_buttons_social">40dp</dimen>
    <dimen name="four">4dp</dimen>
    <dimen name="actionbar_text_size">15sp</dimen>
    <dimen name="actionbar_symbol_size">20sp</dimen>
    <dimen name="actionbar_text_size_title">15sp</dimen>

16dp
16dp
300dp
10dp
1dp
10dp
5dp
8dp
300dp-2个匹配\u父级
第13页第15页
20dp
30dp
第14页第15页
第15页第18页
17 SP 20
2dp
0.5dp
16便士
22 SP 25
30dp
100dp
1dp
80dp
5dp
300dp
15dp
第30页第40页
20sp 20
230dp
200dp
400dp
第11页第13页
22 SP 25
20dp
100dp
30dp
-10dp
11便士
120dp
18便士
40dp
200dp
300dp
11便士
7dp
800dp
14dp
8dp
13dp
3dp
7dp
14dp
4dp
3dp
0.5dp
60dp
@二分之一
40dp
4dp
15便士
20便士
15便士

Color.xml

<resources>
    <color name="light_purple">#ca0571</color>
    <color name="dark_blue">#203142</color>

    <color name="facebook">#3b589a</color>
    <color name="twitter">#4799ef</color>
    <color name="google">#d44837</color>d34836
    <color name="white">#FFFFFF</color>
    <color name="green">#98c269</color>
    <color name="gray">#f6f0ee</color>
    <color name="blue">#56a6db</color>
    <color name="orange">#f26522</color>
    <color name="gray_dark">#b8000000</color>
    <color name="divider_color">#717171</color>

    <color name="menu_bg_color">#444f5b</color>
    <color name="email">#ca0571</color>
</resources

#ca0571
#203142
#3b589a
#4799ef
#D448373D34836
#FFFFFF
#98c269
#f6f0ee
#56a6db
#f26522
#b8000000
#717171
#444f5b
#ca0571

您正在寻找的是iOS上的分段控件,这里是github在android上的一个类似控件

您必须使用选择器drawable。谢谢您的回复。我已经使用了我更新过的选择器,请检查@Chirag SavSanitis只有在按特定布局时才起作用。效果不会保留在特定布局中。那么如何使其保留在该布局中???@Chirag Savsan如果帖子列表\u选择器\u背景可绘制,我将为您创建演示应用程序。抱歉,我不明白您为什么创建home\u button\u left.xml和home\u button\u right.xml??在哪里添加这些布局@Ramesh Kumarbecause我必须使用左侧按钮和右侧按钮的曲线边框。你能分享一下你的尺寸和颜色值吗?我在ViesStatic上发现一个错误。无法解析符号ViewsStatic。你能解释一下我在OnClickListner上要做什么吗@Ramesh KumarThank非常感谢你..@vishal
<?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"
    android:layout_margin="@dimen/ten"
    android:background="@drawable/bg_home_buttons"
    android:orientation="horizontal"
    android:padding="1dp">

    <TextView
        android:id="@+id/tv_pick_up_bak"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:paddingBottom="@dimen/five"
        android:paddingTop="@dimen/five"
        android:text="@string/add_property"
        android:textColor="@android:color/white"
        android:textSize="@dimen/fifteen_sp" />

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

    <TextView
        android:id="@+id/tv_pick_box"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@android:color/white"
        android:gravity="center"
        android:paddingBottom="@dimen/five"
        android:paddingTop="@dimen/five"
        android:text="@string/my_property"
        android:textColor="@color/blue"
        android:textSize="@dimen/fifteen_sp" />

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

    <TextView
        android:id="@+id/tv_engkel_box"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/home_button_right"
        android:gravity="center"
        android:paddingBottom="@dimen/five"
        android:paddingTop="@dimen/five"
        android:text="@string/portfolio"
        android:textColor="@color/blue"
        android:textSize="@dimen/fifteen_sp"
        android:typeface="sans" />

</LinearLayout>
Context context;
 public void changeBackground(int id) {
        if (id == tvPickBag.getId()) {
            tvPickBag.setTextColor(context.getResources().getColor(R.color.white));
            tvPickBox.setTextColor(context.getResources().getColor(R.color.blue));
            tvEngKelBox.setTextColor(context.getResources().getColor(R.color.blue));
            tvPickBag.setBackgroundColor(context.getResources().getColor(android.R.color.transparent));
            tvEngKelBox.setBackground(context.getDrawable( R.drawable.home_button_right));
            tvPickBox.setBackgroundColor(context.getResources().getColor(R.color.white));
        } else if (id == tvEngKelBox.getId()) {
            tvPickBag.setBackground(context.getDrawable(R.drawable.home_button_left));
            tvEngKelBox.setBackgroundColor(context.getResources().getColor(android.R.color.transparent));
            tvPickBox.setBackgroundColor(context.getResources().getColor(R.color.white));
            tvPickBag.setTextColor(context.getResources().getColor(R.color.blue));
            tvPickBox.setTextColor(context.getResources().getColor(R.color.blue));
            tvEngKelBox.setTextColor(context.getResources().getColor(R.color.white));
        } else if (id == tvPickBox.getId()) {
            tvPickBag.setBackground(context.getDrawable( R.drawable.home_button_left));
            tvEngKelBox.setBackground(context.getDrawable( R.drawable.home_button_right));
            tvPickBox.setBackgroundColor(context.getResources().getColor(android.R.color.transparent));
            tvPickBag.setTextColor(context.getResources().getColor(R.color.blue));
            tvPickBox.setTextColor(context.getResources().getColor(R.color.white));
            tvEngKelBox.setTextColor(context.getResources().getColor(R.color.blue));
        }
    }
<resources>
    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>

    <!-- Per the design guidelines, navigation drawers should be between 240dp and 320dp:
         https://developer.android.com/design/patterns/navigation-drawer.html -->
    <dimen name="navigation_drawer_width">300dp</dimen>
    <dimen name="padding_text_view">10dp</dimen>
    <dimen name="border">1dp</dimen>
    <dimen name="ten">10dp</dimen>
    <dimen name="five">5dp</dimen>
    <dimen name="radius_circle_indicator">8dp</dimen>
    <dimen name="width_button_sign_up">300dp</dimen> -2px match_parent
    <dimen name="size_sign_up_text">13sp</dimen>  15
    <dimen name="padding_tutorial_text">20dp</dimen>
    <dimen name="padding_text_view_right">30dp</dimen>
    <dimen name="fifteen_sp">14sp</dimen> 15
    <dimen name="eighteen_sp">15sp</dimen> 18
    <dimen name="twenty_sp">17sp</dimen> 20
    <dimen name="two">2dp</dimen>
    <dimen name="one">0.5dp</dimen>
    <dimen name="sixteen_sp">16sp</dimen>
    <dimen name="twenty_five_sp">22sp</dimen> 25
    <dimen name="margin_">30dp</dimen>
    <dimen name="map_padding">100dp</dimen>
    <dimen name="stroke_width">1dp</dimen>
    <dimen name="loader_size">80dp</dimen>
    <dimen name="loading_width">5dp</dimen>
    <dimen name="loader_layout_size">300dp</dimen>
    <dimen name="fifteen">15dp</dimen>
    <dimen name="fourty_sp">30sp</dimen> 40
    <dimen name="thirty_sp">20sp</dimen> 20
    <dimen name="height_chart">230dp</dimen>
    <dimen name="height_chart_minus">200dp</dimen>
    <dimen name="width_chart">400dp</dimen>
    <dimen name="size_sales_history">11sp</dimen> 13
    <dimen name="size_text_house_price">22sp</dimen> 25
    <dimen name="twenty">20dp</dimen>
    <dimen name="fifty">100dp</dimen>
    <dimen name="top_margin_sales_history_button">30dp</dimen>
    <dimen name="ten_minus">-10dp</dimen>
    <dimen name="size_marker_text">11sp</dimen>
    <dimen name="profile_image_size">120dp</dimen>
    <dimen name="invite_icon_size">18sp</dimen>
    <dimen name="fourty">40dp</dimen>
    <dimen name="minimum_height_listview">200dp</dimen>
    <dimen name="map_size">300dp</dimen>
    <dimen name="bar_text_size">11sp</dimen>
    <dimen name="margin_bar_text">7dp</dimen>
    <dimen name="five_hundred">800dp</dimen>
    <dimen name="padding_text_view_fourteen">14dp</dimen>
    <dimen name="eight">8dp</dimen>
    <dimen name="thirteen">13dp</dimen>
    <dimen name="three">3dp</dimen>
    <dimen name="seven">7dp</dimen>
    <dimen name="margin_home_buttons">14dp</dimen>
    <dimen name="margin_home_buttons_four">4dp</dimen>
    <dimen name="space_to_change_since">3dp</dimen>

    <dimen name="half_divider">0.5dp</dimen>
    <dimen name="size_sales_history_box_width" >60dp</dimen>
    <dimen name="height_line_thick">@dimen/two</dimen>
    <dimen name="height_buttons_social">40dp</dimen>
    <dimen name="four">4dp</dimen>
    <dimen name="actionbar_text_size">15sp</dimen>
    <dimen name="actionbar_symbol_size">20sp</dimen>
    <dimen name="actionbar_text_size_title">15sp</dimen>
<resources>
    <color name="light_purple">#ca0571</color>
    <color name="dark_blue">#203142</color>

    <color name="facebook">#3b589a</color>
    <color name="twitter">#4799ef</color>
    <color name="google">#d44837</color>d34836
    <color name="white">#FFFFFF</color>
    <color name="green">#98c269</color>
    <color name="gray">#f6f0ee</color>
    <color name="blue">#56a6db</color>
    <color name="orange">#f26522</color>
    <color name="gray_dark">#b8000000</color>
    <color name="divider_color">#717171</color>

    <color name="menu_bg_color">#444f5b</color>
    <color name="email">#ca0571</color>
</resources