【问题标题】:How to expand an image relatively to another object in android如何在android中相对于另一个对象扩展图像
【发布时间】:2015-10-27 18:12:33
【问题描述】:

每当按下查看更多按钮时,我一直在尝试使布局自行扩展。第二个标题应该与第一个标题具有相同的框。我想这样当用户按下“查看更多”按钮时,第二个标题仍然位于标题 1 的扩展文本下方。layout wireframe

这是我的布局代码:

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

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

    <!-- The ActionBar -->
    <include
        layout="@layout/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:layout_weight="2.43">
    <!-- The main content view -->
    <RelativeLayout
        android:id="@+id/flContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"

        >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="210dp"
            android:id="@+id/img_chucrute"
            android:background="@drawable/img_chucrute" />

        <ImageView
            android:layout_width="130dp"
            android:layout_height="30dp"
            android:id="@+id/titulo_chucrute"
            android:background="@drawable/chucrute_titulo"
            android:layout_marginTop="227dp"
            android:layout_marginLeft="6dp" />

        <ImageView
            android:layout_width="80dp"
            android:layout_height="25dp"
            android:id="@+id/preco_chucrute"
            android:background="@drawable/preco_chucrute"
            android:layout_marginTop="231dp"
            android:layout_marginLeft="272dp" />

        <ImageView
            android:layout_width="309dp"
            android:layout_height="5dp"
            android:id="@+id/linha_pagkits"
            android:background="@drawable/linha_pagkits"
            android:layout_marginTop="270dp"
            android:layout_marginLeft="27dp" />

        <ImageView
            android:layout_width="100dp"
            android:layout_height="20dp"
            android:id="@+id/titulo_ingredientes"
            android:background="@drawable/ingredientes_titulo"
            android:layout_marginTop="282dp"
            android:layout_marginLeft="29dp" />

        <com.example.renan.chefemcasa.ExpandableTextView
            android:id="@+id/lorem_ipsum"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="302dp"
            android:layout_marginLeft="32dp"
             />

        <ImageView
            android:layout_width="309dp"
            android:layout_height="5dp"
            android:id="@+id/linha_apagar"
            android:background="@drawable/linha_pagkits"
            android:layout_marginLeft="27dp"
            android:layout_marginTop="375dp"
            android:visibility="gone" />

        <ImageView
            android:layout_width="309dp"
            android:layout_height="5dp"
            android:id="@+id/imageView7"
            android:background="@drawable/linha_pagkits"
            android:layout_marginLeft="27dp"
            android:layout_marginTop="370dp" />


    </RelativeLayout>

</ScrollView>


</LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:visibility="visible">
            <ImageButton
                android:layout_width="180dp"
                android:layout_height="50.5dp"
                android:id="@+id/imageButton11"
                android:background="@drawable/botao_adicionais"
                android:layout_marginTop="517dp" />

            <ImageButton
                android:layout_width="180dp"
                android:layout_height="50.5dp"
                android:id="@+id/imageButton12"
                android:background="@drawable/botao_finalizar"
                android:layout_marginTop="-51dp"
                android:layout_marginLeft="180dp"/>
    </LinearLayout>



<!-- The navigation drawer -->
<android.support.design.widget.NavigationView
    android:id="@+id/nvView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@android:color/white"
    app:headerLayout="@layout/nav_header"
    app:menu="@menu/drawer_view" />

感谢您的帮助!

【问题讨论】:

  • 你想展开和折叠一个TextView/一些多个View吗?

标签: android android-layout android-studio


【解决方案1】:

我在您的布局上看不到任何 TextView 元素,而且您拥有的按钮似乎与扩展文本没有不同的目的。一旦你的布局上有一个 Button 和 TextView,你可以在按下按钮时将额外的内容(更长的文本)注入到 TextView 中。

方法如下:

将 TextView 和 Button 添加到您的 layout.xml 中(您可以添加文本格式标记等以满足您的需要)。

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/text_to_expand"
    android:text="short blah blah blah" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/expand_button"
    android:text="expand" />

然后,从您的活动中检索 Button 和 TextView 并添加您想要的行为。

private Button expandButton;
private TextView expandableText;

...

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_init);

    // retrieve the button and the textview from your layout
    expandButton = (Button)findViewById(R.id.expand_button);
    expandableText = (TextView)findViewById(R.id.expand_button);

    // set button functionality when it is clicked
    expandButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // replace the text in your TextView by new longer content
            expandableText.setText("blah, blah, blah, very long, blah, blah");
        }
    });

    ...
}

...

对于额外的点,如果您将此行添加到包含您的 TextView 的布局中,大小的转换将是动画的。

<LinearLayout android:id="@+id/container"
    android:animateLayoutChanges="true"
    ...
/>

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    您可以尝试以下库之一:

    如果您需要他们的帮助,请告诉我们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-26
      • 1970-01-01
      相关资源
      最近更新 更多