开发者

Custom layout based on existed layout

开发者 https://www.devze.com 2023-02-27 06:50 出处:网络
I\'m trying to create custom layout for android. It normally draws on screen, but without inner views. Draws only my group_box.xml. How i can get access from my custom layout to inner views (TextView

I'm trying to create custom layout for android. It normally draws on screen, but without inner views. Draws only my group_box.xml. How i can get access from my custom layout to inner views (TextView with id test) or how to draw they?

main.xml

<my.example.GroupBox
            android:layout_width="match_parent"
            android:layout_height="40sp">
        <TextView android:text="TEST"
                  android:id="@+id/test"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"/>
    </my.example.GroupBox>

group_box.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                style="@style/groupbox">
    <LinearLayout style="@style/groupboxContent"
                  android:id="@+id/content"
                  android:layout_height="match_parent"
                  android:layout_width="match_parent"/>
    <TextView style="@style/groupboxLabel"
              android:id="@+id/caption"
              android:text="@string/visit"/>
</RelativeLayout>

GroupBox.java

public class GroupBox extends LinearLayout {

    public GroupBox(Context context) {
        super(context);
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.group_box, this);
    }

    public GroupBox(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.group_box, this);
   开发者_开发问答 }
}


you can access to the elements via setters and getters.

put this in your GroupBox.java

caption = (TextView) findViewById(R.id.caption);

 public void setLabel(CharSequence text) {
        caption.setText(text);
    }

add an id to your control in xml:

<my.example.GroupBox
            android:layout_width="match_parent"
            android:layout_height="40sp"
android:id="mycontrol">
        <TextView android:text="TEST"
                  android:id="@+id/test"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"/>
    </my.example.GroupBox>

then find your control in the main activity and do this:

yourcontrol = (GroupBox) findViewById(R.id.mycontrol)

yourcontrol.setLabel("test");
0

精彩评论

暂无评论...
验证码 换一张
取 消