我正在开发一个多 fragment 应用程序,但无法获得有关如何执行此操作的任何有用信息:屏幕顶部是一个选项卡栏,您可以在其中切换到不同的 fragment 。下面是我正在处理的 fragment 。我想在最底层绘制内容,上面有一个带有按钮的 EditText。绘制的内容必须在单击按钮时可更新。我目前的想法是将内容元素添加到框架布局并绘制到其中。框架布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/frag_color_bg">
<!-- Content element here -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<EditText
android:id="@+id/frag_text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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;
}
}
};
最佳答案
我会在 View 之上使用 Canvas 。查看这篇文章:http://developer.android.com/guide/topics/graphics/2d-graphics.html#draw-with-canvas
如果你使用它,你将把你自己的扩展 View 放在你的评论的地方。您将创建自己版本的 onDraw() 方法并在您想要重绘时调用 invalidate() (即,当按下按钮时)。像这样将它添加到您的 XML 中:
<com.mypackage.MyView
id="@+id/myView"
... />
希望对您有所帮助!
关于Android:如何在 FrameLayout 元素中绘制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12589450/