gpt4 book ai didi

java - 在其他布局的 LinearLayout 中扩充布局

转载 作者:行者123 更新时间:2023-11-28 12:06:19 24 4
gpt4 key购买 nike

我有这个布局:

ComposeView http://img845.imageshack.us/img845/2121/d6zp.png

2 个边框(左、右)由图标填充。当我触摸其中一个图标时,我会访问其他 Activity 。顶部的黑色条是自定义标题栏。

我需要在我的应用程序中进行所有 Activity 的地方放置清晰的灰色内部空间。所以这个布局类似于菜单布局,在所有 Activity 中都是静态的。

这是布局 xml:

menu_view.xml

<RelativeLayout     
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.MenuView" >


<!-- Show top black custom title bar-->
<include
layout="@layout/custom_tittlebar" >
</include>


<!-- Button columns -->
<LinearLayout
android:id="@+id/lefthandmenu"
android:layout_width="85dip"
android:layout_height="match_parent"
android:layout_below="@id/title"
android:layout_alignParentLeft="true"
android:orientation="vertical"
android:background="@drawable/border_cut" >

<ImageView
... />
...
</LinearLayout>

<LinearLayout
android:id="@+id/righthandmenu"
android:layout_width="85dip"
android:layout_height="match_parent"
android:layout_below="@id/title"
android:layout_alignParentRight="true"
android:orientation="vertical"
android:background="@drawable/border_cut" >

<ImageView
... />
...
</LinearLayout>

<!-- Blank space which will contain other activities -->
<LinearLayout
android:id="@+id/activitycontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toLeftOf="@id/righthandmenu"
android:layout_toRightOf="@id/lefthandmenu"
android:layout_below="@id/title"
android:orientation="vertical" >
</LinearLayout>

这是定义所有图标的 onClickListeners 的类。

菜单 View .java

public class MenuView extends RelativeLayout {

private final LayoutInflater inflater;
Context context;

public MenuViewActivity(Context context, AttributeSet attrs) {
super(context, attrs);

this.context = context;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.menu_view, this, true);


((ImageView)this.findViewById(R.id.navButton)).setOnClickListener(launch_nav);
}

final OnClickListener launch_nav = new OnClickListener() {
@Override
public void onClick(View v) {
getContext().startActivity(new Intent(getContext(), Navigation.class));
}
};

好吧,有了这个(我不确定是否一切正常,也许我对 inflate 方法做错了什么或类似的东西),现在要做的是将其他 Activity 的布局定义在这个里面看法。为此,我写道:

ExampleActivity.java

public class ExampleActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

LinearLayout inside_menu_view = (LinearLayout)findViewById(R.id.activitycontent);

View this_layout = getLayoutInflater().inflate(R.layout.main, inside_menu_view, true);
inside_menu_view.addView(this_layout);

但我在最后一行收到 NullPointerException。所以,在对这个 fragment 进行膨胀时一定有什么地方是错误的。

最佳答案

嘿,使用以下结构。放。在每个 Activity 布局中包含您的通用布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >


<!-- Show top black custom title bar-->
<include
android:id="@+id/ll_top"
layout="@layout/custom_tittlebar"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
</include>



<include
android:id="@+id/ll_left"
layout="@layout/custom_left"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" >


</include>
<include
android:id="@+id/ll_right"
layout="@layout/custom_right"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" >
</include>

<Button
android:id="@+id/btn_center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="center"
android:layout_toRightOf="@+id/ll_left"
android:layout_centerHorizontal="true"
android:layout_below="@+id/ll_top"
></Button>
</RelativeLayout>

关于java - 在其他布局的 LinearLayout 中扩充布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19705485/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com