මම කොහොමද දී තිරස් හා සිරස් පෙළ මධ්යස්ථානය නැහැ TextView, එය හරියටම මැද පෙනී එසේ බව TextViewතුළ Android?
මම කොහොමද දී තිරස් හා සිරස් පෙළ මධ්යස්ථානය නැහැ TextView, එය හරියටම මැද පෙනී එසේ බව TextViewතුළ Android?
Answers:
මම හිතන්නේ ඔබ XML පිරිසැලසුම භාවිතා කරනවා.
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/**yourtextstring**"
/>
ඔබට ගුරුත්වාකර්ෂණය center_verticalහෝ center_horizontalඔබේ අවශ්යතාවය අනුව භාවිතා කළ හැකිය .
ජාවා හි @stealthcopter අදහස් දැක්වූ පරිදි: .setGravity(Gravity.CENTER);
android:gravity="center"). එය තවමත් තිරයේ වම් කෙළවරේ පෙන්වයි. :(
android:layout_gravity="center".
android:gravity="center"
මෙය උපක්රමය කරනු ඇත
ඔබට එය භාවිතයෙන් ගතිකව සැකසිය හැකිය:
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
android:layout_centerInParent="true"
සාපේක්ෂ සැකැස්ම සමඟ භාවිතා කරන විට මෙය ක්රියාත්මක වන්නේ පිරිසැලසුමේ උස සහ පළල රැප්_කොන්ටෙන්ට් ලෙස සකසා ඇති බැවිනි.
TextView. පා full ය සම්පූර්ණ පළල සහ බහු රේඛා සහ ඇතුළත සාධාරණීකරණය කරනු ඇත TextView. එය දර්ශනයේ වම්-යුක්ති සහගත ලෙස දිස්වනු ඇත. අනෙක් පිළිතුරු වල දක්වා ඇති පරිදි ඔබ "ගුරුත්වාකර්ෂණය" භාවිතා කළ යුතුය.
ඔබට සංයෝජනය ද භාවිතා කළ හැකිය:
android:gravity="left|center"
එවිට, පෙළ දර්ශන පළල "fill_parent" ට වඩා වැඩි නම් පෙළ තවමත් වමට පෙළගස්වනු ඇත (ගුරුත්වාකර්ෂණය "කේන්ද්රය" ලෙස පමණක් කේන්ද්රගත නොවේ).
ගුරුත්වාකර්ෂණය යොදන්න:
TextView txtView = (TextView) findViewById(R.id.txtView);
txtView.setGravity(Gravity.CENTER_HORIZONTAL);
සිරස් සඳහා:
txtView.setGravity(Gravity.CENTER_VERTICAL);
XML හි:
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="@string/Hello_World"
/>
මෙය කිරීමට ක්රම දෙකක් තිබේ.
XML කේතයේ පළමුවැන්න. ඔබ ගුණාංගය කෙරෙහි අවධානය යොමු කළ යුතුය Gravity. ඔබට මෙම ගුණාංගය ග්රැෆික් සංස්කාරකයෙන් සොයාගත හැකිය; එය XML EDITOR ට වඩා පහසු විය හැකිය.
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal"
android:text="Your Text"
/>
ඔබගේ විශේෂිත අවස්ථාව සඳහා, ගුරුත්වාකර්ෂණයේ අගයන් වනුයේ:
center_vertical|center_horizontal
චිත්රක සංස්කාරකයේ දී ඔබට හැකි සියලු අගයන් සොයාගත හැකිය, ඒවායේ ප්රති .ල පවා බලන්න.
ඔබ TableLayout භාවිතා කරන්නේ නම්, TableRows හි ගුරුත්වාකර්ෂණය කේන්ද්රයට සැකසීමට වග බලා ගන්න. එසේ නොමැති නම් එය ක්රියා නොකරනු ඇත. මම TableRow හි ගුරුත්වාකර්ෂණය කේන්ද්රයට සකසන තුරු අවම වශයෙන් එය මා සමඟ ක්රියා කළේ නැත.
උදාහරණයක් ලෙස, මේ වගේ:
<TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center">
<TextView android:text="@string/chf" android:id="@+id/tv_chf" android:layout_weight="2" android:layout_height="wrap_content" android:layout_width="fill_parent" android:gravity="center"></TextView>
</TableRow>
ඔබට මේ ආකාරයට පෙළ දර්ශන ගුරුත්වාකර්ෂණය (මධ්ය තිරස් හා මධ්ය සිරස්) සැකසිය යුතුය :
android:layout_centerHorizontal="true"
සහ
android:layout_centerVertical="true"
සහ ගතිකව භාවිතා කිරීම:
textview.setGravity(Gravity.CENTER);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
layout_centerhorizontalසහ layout_centerverticalවිසින් layout_centerHorizontalහා layout_centerVertical(අකුරු වෙත "එච්" හා "V", වෙනත් ආකාරයකින් එය වැඩ ඩිග්රිය).
මගේ මතය අනුව,
android:gravity="center"
වඩා යහපත් වන්නේ,
android:layout_centerInParent="true"
වඩා හොඳ,
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
අවම වශයෙන් පෙළ ආකෘතිකරණය සඳහා.
රේඛීය පිරිසැලසුම සඳහා: XML හි මේ වගේ දෙයක් භාවිතා කරන්න
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal"
android:text="Your Text goes here"
/>
ධාවන වේලාවේදී මෙය සිදු කිරීම සඳහා ඔබේ ක්රියාකාරකම් වලදී මෙවැනි දෙයක් භාවිතා කරන්න
TextView textView1 =(TextView)findViewById(R.id.texView1);
textView1.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
සාපේක්ෂ පිරිසැලසුම සඳහා: XML හි මේ වගේ දෙයක් භාවිතා කරන්න
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:text="Your Text goes here"
/>
ධාවන වේලාවේදී මෙය සිදු කිරීම සඳහා ඔබේ ක්රියාකාරකම් වලදී මෙවැනි දෙයක් භාවිතා කරන්න
TextView textView1 =(TextView)findViewById(R.id.texView1);
RelativeLayout.LayoutParams layoutParams = RelativeLayout.LayoutParams)textView1.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
textView1.setLayoutParams(layoutParams);
XML ගොනුවේ භාවිතා කරන්න.
පිරිසැලසුම් ගොනුව
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="@string/stringtext"/>
හෝ:
ජාවා පන්තිය තුළ මෙය භාවිතා කරන්න
TextView textView =(TextView)findViewById(R.id.texviewid);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
සාපේක්ෂ පිරිසැලසුම සඳහා මෙය භාවිතා කරන්න
android:layout_centerInParent="true"
සහ වෙනත් පිරිසැලසුම් සඳහා
android:gravity="center"
නම් TextView'sඋස හා පළල වේ wrap contentපසුව තුළ ඇති වචනවල TextViewසෑම විටම කේන්ද්ර කළ යුතුය. නමුත් TextView'sපළල match_parentසහ උස නම් match_parentහෝ wrap_contentපහත කේතය ලිවිය යුතුය:
සඳහා RelativeLayout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Hello World" />
</RelativeLayout>
සඳහා LinearLayout:
<?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="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Hello World" />
</LinearLayout>
TextView සඳහා ගුරුත්වාකර්ෂණ කෘති භාවිතා කරන අතර, ඒපීඅයි 17 මට්ටමේ විකල්ප ක්රමයක් ක්රියාත්මක වේ -
textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
වෙනස නොදන්න, නමුත් එය ද ක්රියාත්මක වේ. කෙසේ වෙතත් API මට්ටම 17 හෝ ඊට වැඩි සඳහා පමණි.
තුළ RelativeLayout, එය එය සමඟ හොඳ වනු ඇත.
Buttonඔබට එකතු කළ හැකි තවත් දෙයක්.
පහත දැක්වෙන දේ මට හොඳින් ක්රියාත්මක වේ.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff314859"
android:paddingLeft="16dp"
android:paddingRight="16dp">
<TextView
android:id="@+id/txt_logo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="your text here"
android:textSize="30dp"
android:gravity="center"/>
...other button or anything else...
</RelativeLayout>
ඔබට gravityඔබේ පෙළ දර්ශනයේ කට්ටලය සැකසිය හැකිය CENTER.
ඔබ TableLayout හි TableRow හි පෙළ කේන්ද්රගත කිරීමට උත්සාහ කරන්නේ නම්, මෙන්න මම මෙය සාක්ෂාත් කරගත්තේ:
<TableRow android:id="@+id/rowName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" >
<TextView android:id="@+id/lblSomeLabel"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="100"
android:text="Your Text Here" />
</TableRow>
භාවිත android:textAlignment="center"
<TextView
android:text="HOW WAS\nYOUR\nDAY?"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:id="@+id/textView5"
/>
මෙන්න මම මගේ යෙදුමේ භාවිතා කළ මගේ පිළිතුරයි. එය තිරයේ මධ්යයේ පෙළ පෙන්වයි.
<TextView
android:id="@+id/txtSubject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/subject"
android:layout_margin="10dp"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
ඔබ සාපේක්ෂ පිරිසැලසුම භාවිතා කරන්නේ නම්:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/stringname"
android:layout_centerInParent="true"/>
ඔබ භාවිතා කරන්නේ රේඛීය සැකැස්ම
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/stringname"
android:layout_gravity="center"/>
සරලවම, ඔබේ XML ගොනුවේ, පෙළ දර්ශන ගුරුත්වාකර්ෂණය කේන්ද්රයට සකසන්න:
<TextView
android:gravity="center" />
පෙළ කේන්ද්රගත කර ගැනීමට ඔබට මේ ආකාරයට කළ හැකිය
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />
TextView හි උස සහ පළල එතීෙම් අන්තර්ගතය වන අතර පසුව පෙළ දර්ශනය තුළ ඇති පෙළ සෑම විටම කේන්ද්රගත වී පසුව මව් පිරිසැලසුමෙහි කේන්ද්රගත කරන්න:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Hello.."/>
</RelativeLayout>
රේඛීය සැකැස්ම සඳහා කේතය සමාන වේ:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello.."/>
</LinearLayout>
ව්යාකරණ ගැති මාපිය යනු සාපේක්ෂ ලේයවුට් ජාවා කේතය වන අතර මෙය ක්රියාත්මක වන වේලාවේදී ඔබගේ ක්රියාකාරකම් වලදී මෙවැනි දෙයක් භාවිතා කරයි
TextView textView1 =(TextView)findViewById(R.id.texView1);
RelativeLayout.LayoutParams layoutParams = RelativeLayout.LayoutParams)textView1.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
textView1.setLayoutParams(layoutParams);
මෙම බහුවිධ ක්රමවලින් අපට මෙය සාක්ෂාත් කරගත හැකිය: -
XML ක්රමය 01
<TextView
android:id="@+id/textView"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="@strings/text"
/>
XML ක්රමය 02
<TextView
android:id="@+id/textView"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@strings/text"
/>
XML ක්රමය 03
<TextView
android:id="@+id/textView"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:gravity="center"
android:text="@strings/text"
/>
XML ක්රමය 04
<TextView
android:id="@+id/textView"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:text="@strings/text"
/>
ජාවා ක්රමය 01
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
ජාවා ක්රමය 02
textview.setGravity(Gravity.CENTER);
ජාවා ක්රමය 03
textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
ඔබේ මව් පිරිසැලසුම අනුව පෙළ දර්ශන ගුරුත්වාකර්ෂණය ක්රියා කරයි.
රේඛීය සැකැස්ම :
ඔබ ලීනියර් ලේයූට් භාවිතා කරන්නේ නම් ඔබට ගුරුත්වාකර්ෂණ ගුණාංග දෙකක් ඇන්ඩ්රොයිඩ් සොයාගත හැකිය: ගුරුත්වාකර්ෂණය සහ ඇන්ඩ්රොයිඩ්: පිරිසැලසුම_ගුරුත්වය
android: ගුරුත්වාකර්ෂණය: පෙළ දර්ශනයේ අභ්යන්තර පෙළ සැකැස්ම නිරූපණය කරන අතර ඇන්ඩ්රොයිඩ්: පිරිසැලසුම_ගුරුත්වාකර්ෂණය: මව් දර්ශනයේ පෙළ දර්ශන පිහිටීම නියෝජනය කරයි.
ඔබට පෙළ තිරස් අතට හා සිරස් අතට කේන්ද්රගත කිරීමට අවශ්ය නම් පහත කේතය භාවිතා කරන්න
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="300dp"
android:background="@android:color/background_light"
android:layout_height="300dp">
<TextView
android:layout_width="match_parent"
android:text="Hello World!"
android:gravity="center_horizontal"
android:layout_gravity="center_vertical"
android:layout_height="wrap_content"
/>
</LinearLayout>
සාපේක්ෂ සැකැස්ම :
RelativeLayout භාවිතා කරමින් ඔබට TextView හි පහත දේපල භාවිතා කළ හැකිය
android: ගුරුත්වාකර්ෂණය = පෙළ දර්ශනයේ පෙළ මධ්යස්ථානය සඳහා "කේන්ද්රය".
android: gravity = ඔබට තිරස් අතට කේන්ද්රගත වීමට අවශ්ය නම් "center_horizontal" අභ්යන්තර පෙළ.
android: gravity = ඔබට සිරස් අතට කේන්ද්රගත වීමට අවශ්ය නම් "center_vertical" අභ්යන්තර පෙළ.
android: layout_centerInParent = ඔබට දෙමව්පියන්ගේ දර්ශනයේ මධ්ය ස්ථානයේ පෙළ දර්ශනය අවශ්ය නම් "සත්ය". android: layout_centerHorizontal = ඔබට පෙළ දර්ශනය තිරස් අතට දෙමාපිය දර්ශනයේ කේන්ද්රයේ අවශ්ය නම් "සත්ය". android: layout_centerVertical = දෙමාපිය දර්ශනයේ සිරස් අතට කේන්ද්රගතව ඔබට පෙළ දර්ශනය අවශ්ය නම් "සත්ය".
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="300dp"
android:background="@android:color/background_light"
android:layout_height="300dp">
<TextView
android:layout_width="match_parent"
android:text="Hello World!"
android:gravity="center"
android:layout_centerInParent="true"
android:layout_height="wrap_content"
/>
</RelativeLayout>
මේ ආකාරයෙන් උත්සාහ කරන්න, එය ක්රියාත්මක වනු ඇත
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal|center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
/>
</LinearLayout>