2

嗨,我有一个 m 文件,它是一个相对布局,包含 9 个图像按钮。所以我想通过更改名称和 ID 多次重复使用我的布局。我可以使用gridview,但我的问题是我想使用与使用相对布局相同的自定义设计。所以任何人都可以帮助如何做到这一点。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageButton
        android:id="@+id/day0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignTop="@+id/day1"
        android:layout_marginLeft="14dp"
        android:scaleType="fitXY"
        android:src="@drawable/day0" />

    <ImageButton
        android:id="@+id/day3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/day0"
        android:layout_below="@+id/day0"
        android:layout_marginTop="54dp"
        android:src="@drawable/day3" />

    <ImageButton
        android:id="@+id/day6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/day3"
        android:layout_below="@+id/day3"
        android:layout_marginTop="74dp"
        android:src="@drawable/day6" />

    <ImageButton
        android:id="@+id/day4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/day6"
        android:layout_marginLeft="25dp"
        android:layout_toRightOf="@+id/day3"
        android:src="@drawable/day4" />

    <ImageButton
        android:id="@+id/day7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/day4"
        android:layout_alignTop="@+id/day6"
        android:src="@drawable/day7" />

    <ImageButton
        android:id="@+id/day1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/day4"
        android:layout_alignParentTop="true"
        android:layout_marginTop="39dp"
        android:scaleType="fitXY"
        android:src="@drawable/day1" />

    <ImageButton
        android:id="@+id/day8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/day5"
        android:layout_alignTop="@+id/day7"
        android:src="@drawable/day8" />

    <ImageButton
        android:id="@+id/day2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/day1"
        android:layout_marginLeft="28dp"
        android:layout_toRightOf="@+id/day1"
        android:scaleType="fitXY"
        android:src="@drawable/day2" />

    <ImageButton
        android:id="@+id/day5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/day6"
        android:layout_alignLeft="@+id/day2"
        android:src="@drawable/day5" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView4"
        android:layout_alignBottom="@+id/textView4"
        android:layout_alignLeft="@+id/day7"
        android:text="Emotions"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView7"
        android:layout_alignLeft="@+id/Ans6b"
        android:text="Leadership"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/Ans6b"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/question6a"
        android:layout_alignBottom="@+id/question6a"
        android:layout_alignRight="@+id/day1"
        android:text="gruppstart"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/day3"
        android:layout_toLeftOf="@+id/day4"
        android:text="Adaption"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/day6"
        android:layout_toLeftOf="@+id/day4"
        android:text="Behavior"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_below="@+id/day8"
        android:text="Satisfaction"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/day5"
        android:layout_below="@+id/day5"
        android:text="Insight"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/day2"
        android:layout_below="@+id/day2"
        android:text="relationer"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/question6a"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView7"
        android:layout_below="@+id/day0"
        android:layout_marginLeft="15dp"
        android:text="Goal"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/textView3"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />


</RelativeLayout>

这是我的 xml 文件,我想为不同的活动调用这个 xml 文件,但只需更改名称 ID 和一些显示图像按钮。如何做到这一点任何人都可以建议我

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;

import com.xylem.flygt.R;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

public class MenuActivity extends Activity 
{

    static GridView gridView;

    Bitmap bmp = null;
    public ArrayList<Bitmap> bitmapArray = new ArrayList<Bitmap>();

    Context context; 
    ImageButton day0, day1, day2, day3, day4, day5, day6, day7, day8;

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

        setContentView(R.layout.grid_layout);

        day0 = (ImageButton) findViewById(R.id.day0);
        day1 = (ImageButton) findViewById(R.id.day1);       
        day2 = (ImageButton) findViewById(R.id.day2);
        day3 = (ImageButton) findViewById(R.id.day3);
        day4 = (ImageButton) findViewById(R.id.day4);
        day5 = (ImageButton) findViewById(R.id.day5);
        day6 = (ImageButton) findViewById(R.id.day6);
        day7 = (ImageButton) findViewById(R.id.day7);
        day8 = (ImageButton) findViewById(R.id.day8); 

        gridView = (GridView) findViewById(R.id.grid_view);  
        gridView.setBackgroundColor(Color.parseColor("#E3E4FA"));
        gridView.setOnItemClickListener(new OnItemClickListener() 
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
            {
                System.out.println("Clicked id " + position);

                if(position == 0)
                {
                    System.out.println(" Day 0 ");


                }

                if(position == 1)               {

                    Intent i = new Intent(getApplicationContext(), nextActivity.class);         
                    i.putExtra("dayno", "1");
                    System.out.println(" Day 1 ");
                    startActivity(i);

                }

                if(position == 2)
                {   
                    Intent i = new Intent(getApplicationContext(), nextActivity.class);         
                    i.putExtra("dayno", "2");
                    System.out.println(" Day 2 ");
                    startActivity(i);
                }

                if(position == 3)
                {   Intent i = new Intent(getApplicationContext(), nextActivity.class);         
                    i.putExtra("dayno", "3");
                    System.out.println(" Day 3 ");  
                    startActivity(i);
                }

                if(position == 4)
                {
                    Intent i = new Intent(getApplicationContext(), nextActivity.class);         
                    i.putExtra("dayno", "4");
                    System.out.println(" Day 4 ");          
                    startActivity(i);
                }

                if(position == 5)
                {
                    Intent i = new Intent(getApplicationContext(), nextActivity.class);         
                    i.putExtra("dayno", "5");
                    System.out.println(" Day 5 ");               
                    startActivity(i);
                }       

                if(position == 6)
                {
                    Intent i = new Intent(getApplicationContext(), nextActivity.class);         
                    i.putExtra("dayno", "6");
                    System.out.println(" Day 6");
                    startActivity(i);
                }   

                if(position == 7)
                {
                    Intent i = new Intent(getApplicationContext(), nextActivity.class);         
                    i.putExtra("dayno", "7");
                    System.out.println(" Day 7 ");
                    startActivity(i);
                }   

                if(position == 8)
                {
                    Intent i = new Intent(getApplicationContext(), nextActivity.class);         
                    i.putExtra("dayno", "8");
                    System.out.println(" Day 8 ");
                    startActivity(i);
                }             
            }
        });

        new Checkday().execute(); 
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    class Checkday extends AsyncTask<String, String, String>
    {   
                protected void onPreExecute() 
                {
                    super.onPreExecute();
                }

                @Override
                protected String doInBackground(String... arg0) 
                {
                    return null;
                }

                protected void onPostExecute(String file_url) 
                {   
                            gridView.setAdapter(new CalenderAdaptor(MenuActivity.this));
                }
        }
    }

我已经编写了我的活动 java,它本身在 gridview 中创建了图像按钮。但是我像这样使用我想以这样的方式使用,我在不同的活动中调用我的海关布局,因为布局是相同的,只有 ID 和一些图像会改变。

4

1 回答 1

4

如果我理解正确 - 您有一个带有按钮的自定义设计布局,并且您也想在其他屏幕中使用相同的布局。

您不需要重命名任何内容,只需将布局放在单独的 xml 中,比如说 CommonButtonLayout.xml。这应该包含这样的:

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

.... your layout .....

</merge>

现在您想在其他布局文件中使用它。您需要直接在此处包含此 xml,如下所示。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    ...
    ...
     >

    <include
        android:layout_centerVertical="true"
        layout="@layout/CommonButtonLayout" />
</RelativeLayout>
于 2013-08-20T01:46:21.627 回答