마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

텝호스트 높이 변경하기

0 추천

텝호스트에서 밑에 버튼의 높이를 변경하고 싶은데 어떻게 하나요...?

자바 코드에서 버튼을 누르면 높이를 변경하고 싶습니다.

btnColor를 누르면 색이 변경되고(이건 잘됨), btnHeight를 누르면 높이가 변경되게 하고싶습니다.

public class MainActivity extends TabActivity {
    private TabHost tabHost;
    private Button btnColor,btnHeight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnColor = findViewById(R.id.btnColor);
        btnHeight = findViewById(R.id.btnHeight);

        tabHost = getTabHost();

        TabHost.TabSpec Spec1 = tabHost.newTabSpec("TAG1").setIndicator("1번");
        Spec1.setContent(R.id.test1);
        tabHost.addTab(Spec1);
        TabHost.TabSpec Spec2 = tabHost.newTabSpec("TAG2").setIndicator("2번");
        Spec2.setContent(R.id.test2);
        tabHost.addTab(Spec2);

        tabHost.setCurrentTab(0);

        btnColor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#ff0000"));
            }
        });
        btnHeight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for(int idx=0; idx<tabHost.getTabWidget().getChildCount(); ++idx){
                    tabHost.getTabWidget().getChildAt(idx).getLayoutParams().height=150;
                }
            }
        });

    }
}
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <LinearLayout
                android:id="@+id/test1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <Button
                    android:id="@+id/btnColor"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="배경색 변경" />
            </LinearLayout>
            <LinearLayout
                android:id="@+id/test2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <Button
                    android:id="@+id/btnHeight"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="높이변경" />
            </LinearLayout>
        </FrameLayout>

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#f00fff">
        </TabWidget>
    </LinearLayout>
</TabHost>

 

 

 

 

 

 

 

개미1 (1,260 포인트) 님이 2022년 3월 28일 질문
와 아직도 TabHost를 사용하고 계시는 분이 계시네요. TabLayout + Tab을 사용하는게 일반적인데...

1개의 답변

0 추천

안드로이드에서 View의 width, height를 동적으로 변경할 때 일반적으로 LayoutParam을 이용합니다. 아래처럼 하면 될 것 같은데요.

public class MyActivity extends AppCompatActivity. {

private Button button;

@Override
protected void onCreate(...) {
    super.onCreate(...)
    button = findViewById(...);
    button.setOnClickListner(v -> setButtonHeight(50) );
}

private void setButtonHeight(heightInDp: int) {
    ViewGroup.LayoutParams layoutParams = button.getLayoutParams();

    // button.height는 pixel이므로 dp를 받아서 pixel로 변환.
    int heightInPx = (int) DimensionUtil.dpToPx(this, heightInDp);
    layoutParams.height = heightInPx;
    
    button.setLayoutParams(layoutParams);
}

}

class DimensionUtil {
   public float dpToPx(Context context, float dp) {
        return dp * context.getResources().getDisplayMetrics().density;
    }
}

View.width, height는 pixel이 단위입니다. 따라서 화면에 보이는 단위는 dp를 사용하기 때문에 이걸 pixel로 변경해주어야 합니다. 그래서 DimensionUtil을 추가한 겁니다.

spark (227,830 포인트) 님이 2022년 3월 29일 답변
...