根据用户输入设置 CustomView 的布局
Setting layout of a CustomView based on user input
我有一个扩展 RelativeLayout 的自定义视图。在此 class 的 init(Context context, AttributeSet attrs) 方法中,我通过以下方式设置布局文件:
inflate(getContext(), R.layout.in_session_view_layout, this);
我在这样的列表项中使用此自定义视图:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:verizon="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_margin="5dp">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">
<ImageView
android:id="@+id/flag"
android:layout_width="60dp"
android:layout_height="160dp"
android:layout_below="@id/txt"
android:layout_gravity="center"
android:layout_margin="5dp" />
</FrameLayout>
<com.widgets.FreeBeeNotificationView
android:id="@+id/freebee_pre_session_view"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_gravity="right|top"
android:layout_marginBottom="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="6dp"
verizon:textSize="12sp" />
</FrameLayout>
现在的要求是,这个布局文件需要根据用户输入在自定义视图中动态设置。但是,我在调用自定义视图的 init() 方法后收到用户输入,因为一旦加载上面给出的 FrameLayout 就会调用此方法。
如何根据用户输入将布局文件动态设置为自定义视图?有好心人可以帮帮我吗
谢谢。
希望我已经正确理解了您的要求。如果不是请进一步解释。
你可以做到这一点。
在您的自定义视图中创建一个名为 setLayout(int layoutId) 的方法
public void setLayout(int layoutId)
{
removeAllViews();
LayoutInflater inflater = LayoutInflater.from(getContext());
inflater.inflate(getContext(), layoutId, this);
}
当你有用户输入时,你调用
yourCustomView.setLayout(R.layout.new_layout);
免责声明:所有内容均来自记忆,未经测试,因此可能无法编译。
我有一个扩展 RelativeLayout 的自定义视图。在此 class 的 init(Context context, AttributeSet attrs) 方法中,我通过以下方式设置布局文件:
inflate(getContext(), R.layout.in_session_view_layout, this);
我在这样的列表项中使用此自定义视图:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:verizon="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_margin="5dp">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">
<ImageView
android:id="@+id/flag"
android:layout_width="60dp"
android:layout_height="160dp"
android:layout_below="@id/txt"
android:layout_gravity="center"
android:layout_margin="5dp" />
</FrameLayout>
<com.widgets.FreeBeeNotificationView
android:id="@+id/freebee_pre_session_view"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_gravity="right|top"
android:layout_marginBottom="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="6dp"
verizon:textSize="12sp" />
</FrameLayout>
现在的要求是,这个布局文件需要根据用户输入在自定义视图中动态设置。但是,我在调用自定义视图的 init() 方法后收到用户输入,因为一旦加载上面给出的 FrameLayout 就会调用此方法。
如何根据用户输入将布局文件动态设置为自定义视图?有好心人可以帮帮我吗
谢谢。
希望我已经正确理解了您的要求。如果不是请进一步解释。 你可以做到这一点。
在您的自定义视图中创建一个名为 setLayout(int layoutId) 的方法
public void setLayout(int layoutId)
{
removeAllViews();
LayoutInflater inflater = LayoutInflater.from(getContext());
inflater.inflate(getContext(), layoutId, this);
}
当你有用户输入时,你调用
yourCustomView.setLayout(R.layout.new_layout);
免责声明:所有内容均来自记忆,未经测试,因此可能无法编译。