Android:如何在 FrameLayout 元素中绘制?

Posted

技术标签:

【中文标题】Android:如何在 FrameLayout 元素中绘制?【英文标题】:Android: How to draw in FrameLayout element? 【发布时间】:2012-09-25 19:03:09 【问题描述】:

我正在开发一个多片段应用程序,但无法获得任何好的信息如何执行此操作:屏幕顶部是一个标签栏,您可以在其中切换到不同的片段。下面是我正在处理的片段。我想在最低层上绘制内容,并在上方带有按钮的 EditText。绘制的内容必须在按钮单击时可更新。我目前的想法是将内容元素添加到框架布局并绘制到其中。框架布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout  
xmlns:android="http://schemas.android.com/apk/res/android"  
android:layout_  
android:layout_
android:background="@color/frag_color_bg"> 

<!-- Content element here -->

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_ 
    android:layout_  
    android:orientation="vertical">  

    <EditText
        android:id="@+id/frag_text1"
        android:layout_
        android:layout_
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:background="@color/frag_color_te_bg"
        android:hint="@string/frag_srvhint"
        android:padding="2dp"
        android:textColor="@color/frag_text1"
        android:textSize="12sp">
    </EditText>  

    <Button
        android:id="@+id/frag_plotbtn"
        android:background="@drawable/frag_blue_btn"
        android:text="@string/frag_plotlabel"
        style="@style/frag_btn"
        android:layout_marginTop="0dp">

    </Button>
</LinearLayout>  
</FrameLayout> 

这甚至可能吗?在 map.xml 中放置一个内容元素(在注释的位置)并用 Java 绘制它?如果这是可能的,我必须把哪个内容元素放在那里?一个看法?你有任何示例代码如何做到这一点?

如果这不可能,最好的实现方法是什么?

欢迎任何想法或代码示例。我是 Android 新手,几天来一直在寻找解决方案,但网上的示例并不那么清楚......

非常感谢您的宝贵时间和帮助!

类本身:

public MapFragment extends Fragment 
private final static String LOG_TAG = "Map";
private View mContentView;
private EditText mTxt;
private Activity mAct;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) 
    if (container == null) 
        return null;
    

    mAct = (Activity) getActivity();
    mContentView = (View) inflater.inflate(R.layout.map, container,
            false);
    try 
        mTxt = (EditText) mContentView.findViewById(R.id.frag_text1);

           mContentView.findViewById(R.id.frag_plotbtn).setOnClickListener(
                myOnClickListener);
     catch (Exception e) 
        Log.w(LOG_TAG, e);
    
    return mContentView;


    public OnClickListener myOnClickListener = new OnClickListener() 

    public void onClick(View v) 
        switch (v.getId()) 
        // Plot button
        case R.id.frag_plotbtn:
            // Update drawing here;
            break;
        default:
            break;
        
    
;

【问题讨论】:

【参考方案1】:

我会在视图之上使用画布。看看这篇文章:http://developer.android.com/guide/topics/graphics/2d-graphics.html#draw-with-canvas

如果你使用它,你将把你自己的扩展视图放在你的评论位置。您将创建您自己版本的 onDraw() 方法并在您想要重绘时(即按下按钮时)调用 invalidate()。像这样将它添加到您的 XML 中:

<com.mypackage.MyView
   id="@+id/myView"
   ... />

希望有帮助!

【讨论】:

以上是关于Android:如何在 FrameLayout 元素中绘制?的主要内容,如果未能解决你的问题,请参考以下文章

即使我的活动在 android 2.3.1 中处于纵向模式,如何将 framelayout 设置为横向

Android Honeycomb:如何更改 FrameLayout 中的片段,而不重新创建它们?

Android Honeycomb:如何更改 FrameLayout 中的片段,而不重新创建它们?

Android XML FrameLayout 和 BottomNavigationView

如何在 FrameLayout 中将元素右对齐?

如何使用 FragmentManager 在 FrameLayout 中显示 Fragment A、B、C、...?