我在我的应用程序中使用查看存根。我正在将视图存根扩展到布局,它工作正常。现在,如果您看到 dummy_layout.xml ,我已经给出了 android:layout_height = 20dp 。我需要做的是在点击按钮时将视图存根高度从20 dp更改为Wrap_Content 。我怎样才能做到这一点?
我的最终目标是这样。我有一个按钮。当我第一次点击它时,我需要显示存根的完整布局。再次当我第二次点击它时,我需要设置存根的动画以向下滑动并仅显示其中的一部分。
dummy_layout.xml
<RelativeLayout
android:id="@+id/topLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_watch_pic" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@color/dark_grey"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button" />
</RelativeLayout>
<ViewStub
android:id="@+id/viewStub1"
android:layout="@layout/stub_layout"
android:layout_width="wrap_content"
**android:layout_height="20dp"**
android:layout_alignParentBottom="true"/>
</RelativeLayout>
这是我的班级:
public class DummyFragment extends Fragment {
private View mRootView;
private Context mContext;
private Button mButton;
private boolean isVisible = true;
private RelativeLayout mParentLayout;
@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mRootView = inflater.inflate(R.layout.dummy_layout,
container, false);
mContext = getActivity();
mButton = (Button) mTopLayout.findViewById(R.id.button1);
final ViewStub stub = (ViewStub) mRootView.findViewById(R.id.viewStub1);
**final View inflated = stub.inflate();**
inflated.setVisibility(View.VISIBLE);
final View viewstublayout = mRootView.findViewById(R.id.viewStub1);
mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(isVisible)
{
isVisible=false;
inflated.setVisibility(View.VISIBLE);
}
else
{
isVisible=true;
inflated.setVisibility(View.INVISIBLE);
}
}
});
return mRootView;
}
}
答案 0 :(得分:1)
您可以通过编辑视图LayoutParams来更改视图的宽度/高度。例如:
View stub = findViewById(R.id.viewStub1);
ViewGroup.LayoutParams lp = stub.getLayoutParams();
lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
stub.setLayoutParams(lp);
更好的问题是你为什么要这样做?也许有更好的方法来实现你的目标。
答案 1 :(得分:1)
试试这个
final ViewStub stub = (ViewStub) mRootView.findViewById(R.id.viewStub1);
View view = stub .inflate();
单击内部按钮
LayoutParams params = (LayoutParams) view.getLayoutParams();
params.width = LayoutParams.WRAP_CONTENT;
view.setLayoutParams(params);
答案 2 :(得分:0)
试试这个并让我知道它是否运作良好。
LayoutParams lp = (LayoutParams) (YourView).getLayoutParams();
lp.height = newheight;
YourView.setLayoutParams(lp);