There are two different ways to set the width and height of your layout. For one way, you specify the width and height of the layout in xml file using android:width and android: height attribute. Alternatively, you can set the width and height programmatically. In this Android tip, I am going to show you how to achieve the goal using the second way.
In code, to set the width and height of the layout, you need to use the setLayoutParams() method of the object referencing the layout. The method has parameter that is a
LayoutParams object. With the LayoutParams object you can specify the width and height of the layout.
<LinearLayout
xmlns:android= "http://schemas.android.com/apk/res/android"
xmlns:tools= "http://schemas.android.com/tools"
android:id="@+id/layoutparent"
android:layout_width="0dp"
android:layout_height="0dp"
android:paddingLeft= "@dimen/activity_horizontal_margin"
android:paddingRight= "@dimen/activity_horizontal_margin"
android:paddingTop= "@dimen/activity_vertical_margin"
android:paddingBottom= "@dimen/activity_vertical_margin"
android:orientation="vertical"
android:background="#08088A"
>
<ScrollView
android:layout_width= "match_parent"
android:layout_height= "match_parent"
>
<LinearLayout
android:layout_width= "match_parent"
android:layout_height= "match_parent"
android:orientation= "vertical"
>
<Button
android:id="@+id/button1"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:text="Button"
android:textSize="18sp"
/>
<Button
android:id="@+id/button2"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:text="Button"
android:textSize="18sp"
/>
<Button
android:id="@+id/button3"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:text="Button"
android:textSize="18sp"
/>
<Button
android:id="@+id/button4"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:text="Button"
android:textSize="18sp"
/>
<Button
android:id="@+id/button5"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:text="Button"
android:textSize="18sp"
/>
<Button
android:id="@+id/button6"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:text="Button"
android:textSize="18sp"
/>
</LinearLayout>
</ScrollView>
</LinearLayout>
|
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.