在按钮上更改TextView LayoutParameters单击

时间:2017-03-13 14:31:56

标签: java android android-layout android-fragments

我愿意创建名为阅读更多的按钮,点击TextView的布局应转向WRAP CONTENT阅读更多按钮文字应更改为显示(这是我创建的新按钮,但在xml中设置了可见性GONE,而 READ MORE 具有可见性VISIBLE)< / p>

所以这里是我的代码工作真棒..在第一次点击阅读更多并且第一次点击显示但是之后如果再次我点击阅读更多,只有按钮被 SHOW LESS 替换,布局保持不变。

  • 注意:我放置TextView位于LinearLayout 内的CardView及其片段

XML CODE

<LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:orientation="vertical">


                                <TextView
                                    android:id="@+id/view_project_description"
                                    android:layout_width="match_parent"
                                    android:layout_height="80dp"
                                    android:padding="15dp"
                                    android:text="No Description"
                                    android:textColor="@color/black_overlay"
                                    android:textSize="18dp" />


                                <View
                                    android:layout_width="match_parent"
                                    android:layout_height="12dp"
                                    android:background="#44eaeaea" />

                                <Button
                                    android:id="@+id/view_project_readmore_button"
                                    android:layout_width="match_parent"
                                    android:layout_height="27dp"
                                    android:background="#55f4f4f4"
                                    android:layout_marginLeft="75dp"
                                    android:layout_marginRight="75dp"
                                    android:text="Read More >>>"
                                    android:textColor="#3143b3"
                                    android:textSize="13sp" />

                                <Button
                                    android:id="@+id/view_project_showless_button"
                                    android:layout_width="match_parent"
                                    android:layout_height="27dp"
                                    android:background="#55f4f4f4"
                                    android:layout_marginLeft="75dp"
                                    android:layout_marginRight="75dp"
                                    android:text="Show Less >>>"
                                    android:textColor="#3143b3"
                                    android:textSize="13sp"
                                    android:visibility="gone"/>




                            </LinearLayout>

JAVA BUTTON CODE

final Button view_project_readmore =(Button)getView().findViewById(R.id.view_project_readmore_button);
    final Button view_project_showless = (Button)getView().findViewById(R.id.view_project_showless_button);
    view_project_showless.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            project_desc.setEnabled(false);
            project_desc.setVisibility(View.GONE);
            final float scale = getContext().getResources().getDisplayMetrics().density;
            int pixels = (int) (80 * scale + 0.5f);
            project_desc.setHeight(500);
            project_desc.setText("This is less Description Now");
            project_desc.setEnabled(true);
            project_desc.setVisibility(View.VISIBLE);
            view_project_readmore.setVisibility(View.VISIBLE);
            view_project_showless.setVisibility(View.GONE);


        }
    });

    view_project_readmore.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            project_desc.setEnabled(false);
            //project_desc.setVisibility(View.GONE);
            Typeface typeface=Typeface.createFromAsset(getActivity().getAssets(), "fonts/Raleway-Medium.ttf");
            project_desc.setTypeface(typeface);
            project_desc.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec non sapien tellus. Suspendisse non sapien nulla. Maecenas ornare velit sit amet consequat hendrerit. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec egestas dignissim enim. Nunc non egestas lacus. Aliquam rhoncus nec urna eu semper. In varius justo augue, eu tincidunt tellus ultricies nec. Pellentesque vel metus sapien.\n" +
                    "\n" +
                    "Morbi venenatis ultrices felis et sodales. Suspendisse aliquet justo nec gravida viverra. Nulla condimentum mi ac purus feugiat semper. Ut libero nunc, molestie ac dignissim quis, laoreet vel sapien. Morbi porttitor pulvinar mi, non consectetur lectus posuere ac. Morbi blandit nisl eu diam pretium, maximus mattis orci lobortis. Donec ut tincidunt erat. Donec pharetra, sapien eget mollis vestibulum, felis nisl finibus sapien, tempus laoreet turpis ipsum fermentum eros. Praesent eu nulla facilisis, aliquam massa a, hendrerit risus. Proin at mi odio. Quisque semper, nunc sit amet molestie mattis, quam orci commodo ipsum, ut congue risus dolor eget tellus. Nullam ac mauris in eros condimentum lobortis eget eget ipsum. Lorem ipsum dolor sit amet, consectetur adipiscing elit.");

            ViewGroup.LayoutParams params = project_desc.getLayoutParams();
            params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            project_desc.setLayoutParams(params);
            project_desc.setEnabled(true);
            //project_desc.setVisibility(View.VISIBLE);
            view_project_readmore.setVisibility(View.GONE);
            view_project_showless.setVisibility(View.VISIBLE);



        }
    });

1 个答案:

答案 0 :(得分:2)

通过show less按钮的点击监听器中的布局参数设置高度可以解决问题。

所以替换这个:

project_desc.setHeight(500);

有了这个:

ViewGroup.LayoutParams params = project_desc.getLayoutParams();
params.height = 500;                
project_desc.setLayoutParams(params);

或者如果你想让它更短,这也可以:

project_desc.getLayoutParams().height = 500;