在Android中重用布局(<include>)</include>

时间:2012-09-17 11:58:06

标签: android android-layout

我对android更新。在我的项目中,我有一个共同的布局适用于所有活动。我试图在Android中重用我的布局。以下是我的代码

MainActivity

public class MainActivity extends Activity implements OnClickListener {


    EditText emailEdit, passwordEdit;
    Button loginButton;

    TitleBarLayout titlebarLayout;
    String r;
    String rr;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_activity);

        emailEdit = (EditText) findViewById(R.id.etEmail);
        passwordEdit = (EditText) findViewById(R.id.etPassword);

        loginButton = (Button) findViewById(R.id.loginButton);
        loginButton.setOnClickListener(this);

        //titlebarLayout=(TitleBarLayout) findViewById(R.id.titlebar);
        titlebarLayout=new TitleBarLayout(MainActivity.this);
        titlebarLayout.setLeftButtonText("Refresh");
        titlebarLayout.setRightButtonText("Logout");
        titlebarLayout.setTitle("iProtect");

        OnClickListener listener=new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(v.getId()==R.id.left_button)
                {}
                else if(v.getId()==R.id.right_button)
                {}

            }
        };
        titlebarLayout.setLeftButtonOnClickListener(listener);
        titlebarLayout.setRightButtonOnClickListener(listener);
    }
}

login_activity.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"
    android:background="@color/layout_bg_color">

    <include layout="@layout/titlebar_layout" 
        android:id="@+id/titlebar"/>

    <EditText
        android:id="@+id/etEmail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@layout/titlebar_layout"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dp"
        android:layout_marginTop="100dip"
        android:background="@drawable/emailedit"
        android:inputType="textEmailAddress"
        android:maxEms="20"
        android:paddingLeft="70dip"
        android:singleLine="true"
        android:text="user@gmail.com" />

    <EditText
        android:id="@+id/etPassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/etEmail"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dp"
        android:layout_marginTop="0dip"
        android:background="@drawable/passwordedit"
        android:inputType="textPassword"
        android:maxEms="20"
        android:paddingLeft="70dip"
        android:singleLine="true"
        android:text="123456" />

     <Button
        android:id="@+id/loginButton"
        android:layout_width="@dimen/login_button_width"
        android:layout_height="@dimen/login_button_height"
        android:layout_below="@+id/etPassword"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dip"
        android:text="Login" 
        android:textStyle="bold"
        android:background="@android:color/white"/>

    <ImageView
        android:id="@+id/ivFooter"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_below="@+id/loginButton"
        android:layout_marginTop="360dip"
        android:adjustViewBounds="true"
        android:baselineAlignBottom="false"
        android:cropToPadding="false"
        android:src="@drawable/footer" />

</RelativeLayout>

titleBarlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/titlebar_height"
    android:background="@color/title_bg_color"
    android:gravity="center_vertical"
    android:orientation="horizontal">

    <Button
        android:id="@+id/left_button"
        android:layout_width="@dimen/titlebar_button_width"
        android:layout_height="@dimen/titlebar_button_height"
        android:background="#000000"/>

    <TextView
        android:id="@+id/title_textview"
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="@string/app_name"
        android:textColor="@android:color/white"
        android:textStyle="bold" />

    <Button
        android:id="@+id/right_button"
        android:layout_width="@dimen/titlebar_button_width"
        android:layout_height="@dimen/titlebar_button_height" />

</LinearLayout>

和TitleBarLayout.java

public class TitleBarLayout extends LinearLayout {

    private WeakReference<Activity> activityRef;
    //Button leftButton, rightButton;
    private View contentView;
    Button leftButton,rightButton;
    TextView titletext;

    public TitleBarLayout(Activity a) {
        super(a);
        Log.i("TitleBar Layout", "Inside constructor");
        activityRef = new WeakReference<Activity>(a);
        inflateViewsFromXml();
        setListenersOnViews();
        setValuesOnViews();
    }

    private void setValuesOnViews() {
        // TODO Auto-generated method stub
    }

    private void setListenersOnViews() {
        // TODO Auto-generated method stub


    }

    private void inflateViewsFromXml() {
        Activity a = activityRef.get();
        LayoutInflater inflater = (LayoutInflater) a.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        contentView = inflater.inflate(R.layout.titlebar_layout, null);
    }

    public void setLeftButtonText(int resID) {

    }

    public void setLeftButtonText(String resID) {


        Log.v("Button", "Button Text="+resID);
        if (contentView != null) {
            leftButton = (Button)contentView.findViewById(R.id.left_button);
            Log.i("ERROR", "Inside setLeftButtonText");
            leftButton.setText(""+resID);
            leftButton.setTextColor(Color.WHITE);
        }
    }

    public void setLeftButtonOnClickListener(View.OnClickListener listener) {
        leftButton.setOnClickListener(listener);
    }

    public void setRightButtonText(int resID) {

    }

    public void setRightButtonText(String resID) {

        Log.v("Button", "Button Text="+resID);
        if (contentView != null) {
            Log.i("ERROR", "Inside setRightButtonText");
            rightButton = (Button)contentView.findViewById(R.id.right_button);
            rightButton.setTextColor(Color.BLACK);
            rightButton.setText(""+resID);

        }
    }

    public void setRightButtonOnClickListener(View.OnClickListener listener) {

        rightButton.setOnClickListener(listener);

    }

    public void setTitle(int resID) {

    }

    public void setTitle(String resID) {

        //@string/app_name
        Log.v("TextView", "Text="+resID);
        if (contentView == null) {
            Log.i("ERROR", "Inside setTitle");
            titletext = (TextView)contentView.findViewById(R.id.title_textview);
            titletext.setText(""+resID);
        }
    }
}

我想在运行时在按钮上添加文字。但是我无法得到它?我可以在Logcat上看到文字而不是按钮吗?

2 个答案:

答案 0 :(得分:0)

您正在将布局扩展到变量视图'contentView'。 但是因为你的类是你在xml文件中膨胀的视图。你应该充气如下:

    inflater.inflate(R.layout.titlebar_layout, this);

然后屏幕上会显示布局。

这样,inflate会将夸大的视图添加到您的课程所在的LinearLayout

答案 1 :(得分:0)

这不正确。 Android已经在膨胀标题栏,因此您不必再次充气。相反,您可以使用findViewById来获取标题栏的XML信息。我认为您可以使用@ + id对您提供给标记

的内容执行此操作

更新:因为我误读了代码

而编辑了正确性