切换可点击的LinearLayout

时间:2019-05-06 19:05:27

标签: c# xamarin.android

我有问题。我创建了一个带有自定义xml选择器的可点击LinearLayout。现在,当我专注于LinearLayout时,设计会像我想要的方式一样发生变化。但是当我释放LinearLayout时,它又恢复了正常。 我想创建某种切换按钮,但要使用LinearLayout 。这是LinearLayout的完整代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0e2a56"
    android:weightSum="100">
    <LinearLayout
        android:orientation="vertical"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_weight="10"
        android:layout_height="0dp"
        android:id="@+id/linearLayout2" >
        <TextView
            android:text="Choose your shape:"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/textView1"
            android:textColor="#ffffffff"
            android:textStyle="bold"
            android:textSize="30dp"
            android:gravity="center_vertical" />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_weight="100"
        android:layout_height="0dp"
        android:id="@+id/linearLayout1" >
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="0dp"
            android:layout_weight="50"
            android:layout_height="120dp"
            android:background="@drawable/TriangleShapeButton"
            android:id="@+id/btnTriangle"
            android:clickable="true"
            android:focusable="true">
            <ImageView
                android:src="@drawable/triangle3"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/imageView1"
                android:layout_gravity="center|center_horizontal" />
        </LinearLayout>
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="0dp"
            android:layout_weight="50"
            android:layout_height="120dp"
            android:background="@drawable/HexagonShapeButton"
            android:id="@+id/btnHexagon"
            android:clickable="true"
            android:focusable="true">
            <ImageView
                android:src="@drawable/hexagon2"
                android:layout_width="90dp"
                android:layout_height="match_parent"
                android:id="@+id/imageView1"
                android:layout_gravity="center_horizontal|center_vertical"
                android:scaleX="1.2"
                android:scaleY="1.2"  />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

这是自定义XML:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_pressed="false">
    <layer-list>
      <item android:right="5dp" android:top="5dp" android:bottom="5dp" android:left="5dp">
        <shape>
          <corners android:radius="2dp"/>
          <solid android:color="#000000"/>
        </shape>
      </item>
      <item android:right="2dp" android:top="2dp" android:bottom="2dp" android:left="2dp">
        <shape>
          <gradient android:angle="270" android:endColor="#082e72" android:startColor="#104bb2"/>
          <stroke android:width="1dp" android:color="#000000"/>
          <corners android:radius="4dp"/>
          <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp"/>
        </shape>
      </item>
    </layer-list>
  </item>
  <item android:state_pressed="true">
    <layer-list>
      <item android:right="5dp" android:top="5dp">
        <shape>
          <corners android:radius="2dp"/>
          <solid android:color="#D6D6D6"/>
        </shape>
      </item>
      <item android:right="2dp" android:top="2dp" android:bottom="2dp" android:left="2dp">
        <shape>
          <gradient android:angle="270" android:endColor="#6b9fff" android:startColor="#4064a5"/>
          <stroke android:width="3dp" android:color="#FFFFFF"/>
          <corners android:radius="4dp"/>
          <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp"/>
        </shape>
      </item>
    </layer-list>
  </item>
</selector>

我认为我可以使用以下代码:

LinearLayout btnTriangle = view.FindViewById<LinearLayout>(Resource.Id.btnTriangle);
btnTriangle.Click += (sender, e) => {
    btnTriangle.Pressed = true;
};

但这不起作用! 谁能帮我吗?

1 个答案:

答案 0 :(得分:0)

您在android:layout_width="0dp"中设置了LinearLayout,因此看不到图像。 我将值从0dp更改为120dp

<LinearLayout
android:orientation="vertical"
android:layout_width="120dp"
android:layout_weight="50"
android:layout_height="120dp"
android:background="@drawable/TriangleShapeButton"
android:id="@+id/btnTriangle"
android:clickable="true"
android:focusable="true">
<ImageView
    android:src="@drawable/triangle3"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:id="@+id/imageView1"
    android:layout_gravity="center|center_horizontal" />
 </LinearLayout>

我不知道您的<ImageView android:src="@drawable/triangle3",因此我找到了另一张图片替换它,该图片正在运行GIF。 enter image description here

更新:您需要以下截图之类的结果吗? enter image description here

有布局代码(我将android:layout_width0dp更改为android:layout_width="wrap_content"

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

 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#0e2a56"
 android:weightSum="100">

 <LinearLayout
    android:orientation="vertical"
    android:minWidth="25px"
    android:minHeight="25px"
    android:layout_width="match_parent"
    android:layout_weight="10"
    android:layout_height="wrap_content"
    android:id="@+id/linearLayout2" >
    <TextView
        android:text="Choose your shape:"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/textView1"
        android:textColor="#ffffffff"
        android:textStyle="bold"
        android:textSize="30dp"
        android:gravity="center_vertical" />
</LinearLayout>

<LinearLayout
    android:orientation="horizontal"
    android:minWidth="25px"
    android:minHeight="25px"
    android:layout_width="match_parent"
    android:layout_weight="100"
    android:layout_height="0dp"
    android:id="@+id/linearLayout1" >
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_weight="50"
        android:layout_height="120dp"
        android:background="@drawable/TriangleShapeButton"
        android:id="@+id/btnTriangle"
        android:clickable="true"
        android:focusable="true">
         <ImageView
            android:src="@drawable/ring"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/imageView1"
            android:layout_gravity="center|center_horizontal" />
    </LinearLayout>


    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_weight="50"
        android:layout_height="120dp"
        android:background="@drawable/TriangleShapeButton"
        android:id="@+id/btnHexagon"
        android:clickable="true"
        android:focusable="true">
         <ImageView
            android:src="@drawable/ring"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/imageView2"
            android:layout_gravity="center|center_horizontal" />

    </LinearLayout>
</LinearLayout>

</LinearLayout>
</LinearLayout>

更新:在selector 中设置后,正在运行GIF

enter image description here

选择器的代码

<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected ="false" >
<layer-list>
  <item android:right="5dp" android:top="5dp" android:bottom="5dp" 
 android:left="5dp">
    <shape>
      <corners android:radius="2dp"/>
      <solid android:color="#000000"/>
    </shape>
  </item>
  <item android:right="2dp" android:top="2dp" android:bottom="2dp" 
   android:left="2dp">
    <shape>
      <gradient android:angle="270" android:endColor="#082e72" 
      android:startColor="#104bb2"/>
      <stroke android:width="1dp" android:color="#000000"/>
      <corners android:radius="4dp"/>
      <padding android:bottom="10dp" android:left="10dp" 
      android:right="10dp" android:top="10dp"/>
    </shape>
  </item>
 </layer-list>
 </item>
 <item android:state_selected ="true">
 <layer-list>
  <item android:right="5dp" android:top="5dp">
    <shape>
      <corners android:radius="2dp"/>
      <solid android:color="#D6D6D6"/>
    </shape>
  </item>
  <item android:right="2dp" android:top="2dp" android:bottom="2dp" 
   android:left="2dp">
    <shape>
      <gradient android:angle="270" android:endColor="#6b9fff" 
       android:startColor="#4064a5"/>
      <stroke android:width="3dp" android:color="#FFFFFF"/>
      <corners android:radius="4dp"/>
      <padding android:bottom="10dp" android:left="10dp" 
    android:right="10dp" android:top="10dp"/>
    </shape>
  </item>
</layer-list>
</item>
</selector>

当我单击图片时,更改状态。

  LinearLayout btnTriangle = FindViewById<LinearLayout> 
 (Resource.Id.btnTriangle);

        btnTriangle.Click += (sender, e) =>
        {
            btnTriangle.Selected = !btnTriangle.Selected;

        };