【问题标题】:How to draw a border around a mapview如何在地图视图周围绘制边框
【发布时间】:2011-07-04 17:00:46
【问题描述】:

单击按钮时,我试图在我的 mapView 周围画一个边距。

所以这是我尝试过的,但它不起作用。地图视图位于相对布局内。

LayoutInflater inflater = getLayoutInflater();

LinearLayout mView = (LinearLayout) inflater.inflate(
                        R.layout.map_view_create_ps, mapView, false);

GeoPoint viewLoc = mapView.getMapCenter();
MapView.LayoutParams params = new MapView.LayoutParams(
                        LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT,
                        viewLoc,

MapView.LayoutParams.BOTTOM_CENTER);
                mView.setLayoutParams(params);
                mapView.addView(mView);
                mView.setVisibility(View.VISIBLE);

点击按钮时调用上述方法

我的 create_ps_layout 就是这样

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@android:color/transparent">
<LinearLayout android:layout_width="fill_parent"
    android:id="@+id/linearLayout1" android:orientation="vertical"
    android:layout_height="fill_parent"
    android:background="@drawable/rounded_boarder"></LinearLayout>

 </LinearLayout>

而可绘制的背景是这样的

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<solid android:color="@color/translucent_black" />
<corners android:bottomRightRadius="30dp"
    android:bottomLeftRadius="30dp" android:topLeftRadius="30dp"
    android:topRightRadius="30dp" />
    <stroke android:width="2dip" android:color="#FFB600"/>
</shape>

这不起作用,但是你们能否指出我在选择按钮时如何在地图视图周围绘制边框的正确方向?

【问题讨论】:

    标签: java android border android-mapview


    【解决方案1】:

    将你的mapview放在一个RelativeLayout中,并设置RelativeLayout的padding属性如下:

          <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical" android:layout_weight="1"
                android:background="#444" android:padding="2dp">
    
                <com.google.android.maps.MapView
                    android:id="@+id/mapView"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:apiKey="YOUR-MAP-KEY"
                    android:clickable="true"/>
            </RelativeLayout>
    

    【讨论】:

      【解决方案2】:

      按照以下步骤操作:

      1- 使用以下代码在可绘制对象中创建 image_border.xml:

       <?xml version="1.0" encoding="utf-8"?>
       <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
       <item>
          <shape android:shape="rectangle" >
              <gradient
                  android:angle="90"
                  android:centerColor="#30ffffff"
                  android:endColor="#30ffffff"
                  android:startColor="#30ffffff" />
          </shape>
      </item>
      <item
          >
          <shape android:shape="rectangle" >
              <solid android:color="#30ffffff" />
          </shape>
      </item>
      

      2- 使用以下代码在可绘制对象中创建另一个 XML image_borderless.xml:

       <?xml version="1.0" encoding="utf-8"?>
       <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
      <item>
          <shape android:shape="rectangle" >
              <gradient
                  android:angle="90"
           ></gradient>
          </shape>
      </item>
      <item
        >
      <shape android:shape="rectangle" >
         </shape>
      </item>
      

      3- 在您的 onClick 实现中添加以下代码:

             wp1.setOnClickListener(new OnClickListener() {
         @Override
          public void onClick(View v) {
      
      
             //wp1 to add a boarder if selected 
            wp1.setBackgroundDrawable(getResources().getDrawable(
                          R.drawable.image_border));
                  wp1.setPadding(8, 8, 8, 8);
            //wp3 to hide the boarder if not selected 
             wp3.setBackgroundDrawable(getResources().getDrawable(
                          R.drawable.image_borderless));
      
             }
          });
      

      【讨论】:

        【解决方案3】:

        按如下方式创建图层列表。并将其作为背景添加到您的相关布局中。这将给出一个带有阴影的边框

        这里是更多详细信息的链接http://www.uifuel.com/android-creating-a-drop-shadow-in-xml-layout/

         <?xml version="1.0" encoding="utf-8"?>
            <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        
             <!-- Drop Shadow Stack -->
             <item>
                <shape>
                    <padding android:top="1dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
                    <solid android:color="#00CCCCCC" />
                </shape>
            </item>
             <item>
                <shape>
                    <padding android:top="1dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
                    <solid android:color="#10CCCCCC" />
                </shape>
            </item>
             <item>
                <shape>
                    <padding android:top="1dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
                    <solid android:color="#20CCCCCC" />
                </shape>
            </item>
             <item>
                <shape>
                    <padding android:top="1dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
                    <solid android:color="#30CCCCCC" />
                </shape>
            </item>
            <item>
                <shape>
                    <padding android:top="1dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
                    <solid android:color="#50CCCCCC" />
                </shape>
            </item>
        
            <!-- Background -->
            <item>
            <shape>
                    <solid android:color="@color/white" />
                <corners android:radius="3dp" />
            </shape>
            </item>
        </layer-list>
        

        【讨论】:

          【解决方案4】:

          创建一个简单的描边矩形可绘制(一个 xml 形状)并在地图视图上切换它。 这是一个示例。为了简单起见,我使用 SurfaceView 而不是 MapView,它的工作原理应该是一样的。

          res/drawable/red_frame.xml:

          <?xml version="1.0" encoding="utf-8"?>
          <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
              <stroke android:color="#ff0000" android:width="3dp"/>
              <padding android:left="3dp" android:top="3dp" android:right="3dp" android:bottom="3dp" />
          </shape>
          

          res/layout/activity_main.xml:

          <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                          android:layout_width="match_parent"
                          android:layout_height="match_parent">
          
              <CheckBox
                      android:id="@+id/frame_switch"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_centerHorizontal="true"
                      android:text="Show frame" />
          
              <SurfaceView
                      style="@android:style/TextAppearance.Large"
                      android:id="@+id/target"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      android:layout_below="@id/frame_switch"/>
          
          </RelativeLayout>
          

          最后是 Java 粘合剂 - MainActivity.java:

          public class MainActivity extends Activity {
          
              @Override
              protected void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.activity_main);
          
                  final View target = findViewById(R.id.target);
                  final CheckBox checkbox = (CheckBox) findViewById(R.id.frame_switch);
          
                  checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                      @Override
                      public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                          target.setBackgroundResource(isChecked ? R.drawable.red_frame : 0);
                      }
                  });
              }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2020-05-03
            • 1970-01-01
            • 1970-01-01
            • 2011-01-02
            • 2017-08-08
            • 1970-01-01
            • 2011-01-19
            相关资源
            最近更新 更多