使用java更改自定义操作栏的高度

时间:2015-05-07 11:41:28

标签: java android android-layout android-actionbar

我遇到了问题。我是初级Android开发人员。我想在点击事件上更改自定义操作栏的高度。但我找不到办法 如何改变动作栏的高度。我看到其他相关的帖子,但没有找到我需要的确切内容。 我想在点击事件时切换操作栏的隐藏布局部分。动作的高度应该每次都适合内容大小。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_gravity="center"
        android:background="@drawable/header_bg"
        android:orientation="horizontal"
        android:padding="5dp"
        android:visibility="visible"
        android:weightSum="5" >
        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2.69"
            android:src="@drawable/header_image" />
        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.4"
            android:contentDescription="Seperator"
            android:src="@drawable/header_seperator" />
        <ImageView
            android:id="@+id/iv_show_searchbar"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="0.8"
            android:contentDescription="search"
            android:src="@drawable/header_search" />
        <ImageView
            android:id="@+id/iv_hidesearchbar"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="0.8"
            android:contentDescription="search"
            android:src="@drawable/header_search_active"
            android:visibility="gone" />
        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.4"
            android:contentDescription="Seperator"
            android:src="@drawable/header_seperator" />
        <ImageView
            android:id="@+id/iv_header_navigation"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="0.6"
            android:contentDescription="navigation"
            android:src="@drawable/header_navigation" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="57dp"
        android:background="#F0F0F0"
        android:orientation="horizontal"
        android:padding="4dp"
        android:weightSum="5"
        android:visibility="gone" >
        <EditText
            android:id="@+id/editText1"
            android:layout_width="0dp"
            android:layout_height="48dp"
            android:layout_marginRight="10dp"
            android:layout_weight="3"
            android:background="@drawable/fields"
            android:ems="10"
            android:hint="Keyword"
            android:padding="15dp"
            android:textSize="20sp" >
            <requestFocus />
        </EditText>
        <Spinner
            android:id="@+id/spinner1"
            android:layout_width="0dp"
            android:layout_height="48dp"
            android:layout_weight="2.0"
            android:background="@drawable/header_language"
            android:paddingLeft="10dp"
            android:paddingRight="10dp" />
    </LinearLayout>

</LinearLayout>

在点击事件中,我想切换第二个内部线性布局。可能吗?或者我应该在活动中使用片段吗?

1 个答案:

答案 0 :(得分:0)

如果你想要的是你想让第二个LinearLayout(具有可见性的那个:“消失了”)在点击事件中可见。您首先要为该布局设置ID并执行以下操作:

LinearLayout yourLinearLayout =(LinearLayout)findViewById(R.id.yourLinearLayoutId);

并在点击事件中

yourLinearLayout.setVisibility(View.Visible);

将使您的布局显示,并且因为父亲具有layout_height =“wrap_content”,它将适合孩子可见时的大小。